Class: RSpec::OpenAPI::SchemaFile
- Inherits:
-
Object
- Object
- RSpec::OpenAPI::SchemaFile
- Defined in:
- lib/rspec/openapi/schema_file.rb
Overview
TODO: Support JSON
Instance Method Summary collapse
-
#edit(&block) ⇒ Hash
Reads the existing spec, lets the block mutate it, writes the result back and returns the (symbolized) spec so it can be mirrored to other files.
-
#initialize(path) ⇒ SchemaFile
constructor
A new instance of SchemaFile.
-
#write(spec) ⇒ Object
Writes an already-built spec to this file, choosing the format from the file extension.
Constructor Details
#initialize(path) ⇒ SchemaFile
Returns a new instance of SchemaFile.
13 14 15 |
# File 'lib/rspec/openapi/schema_file.rb', line 13 def initialize(path) @path = path end |
Instance Method Details
#edit(&block) ⇒ Hash
Reads the existing spec, lets the block mutate it, writes the result back and returns the (symbolized) spec so it can be mirrored to other files.
20 21 22 23 24 25 26 |
# File 'lib/rspec/openapi/schema_file.rb', line 20 def edit(&block) spec = read block.call(spec) spec ensure write(spec) end |
#write(spec) ⇒ Object
Writes an already-built spec to this file, choosing the format from the file extension.
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/rspec/openapi/schema_file.rb', line 31 def write(spec) stringified = RSpec::OpenAPI::KeyTransformer.stringify(spec) FileUtils.mkdir_p(File.dirname(@path)) output = if json? JSON.pretty_generate(stringified) else prepend_comment(YAML.dump(stringified)) end File.write(@path, output) end |