class RSpec::OpenAPI::SchemaFile

TODO: Support JSON

Public Class Methods

new(path) click to toggle source

@param [String] path

# File lib/rspec/openapi/schema_file.rb, line 7
def initialize(path)
  @path = path
end

Public Instance Methods

edit(&block) click to toggle source
# File lib/rspec/openapi/schema_file.rb, line 11
def edit(&block)
  spec = read
  block.call(spec)
ensure
  write(spec)
end

Private Instance Methods

prepend_comment(content) click to toggle source
# File lib/rspec/openapi/schema_file.rb, line 32
def prepend_comment(content)
  return content if RSpec::OpenAPI.comment.nil?

  comment = RSpec::OpenAPI.comment.dup
  unless comment.end_with?("\n")
    comment << "\n"
  end
  "#{comment.gsub(/^/, '# ').gsub(/^# \n/, "#\n")}#{content}"
end
read() click to toggle source

@return [Hash]

# File lib/rspec/openapi/schema_file.rb, line 21
def read
  return {} unless File.exist?(@path)
  YAML.load(File.read(@path))
end
write(spec) click to toggle source

@param [Hash] spec

# File lib/rspec/openapi/schema_file.rb, line 27
def write(spec)
  FileUtils.mkdir_p(File.dirname(@path))
  File.write(@path, prepend_comment(YAML.dump(spec)))
end