class SchemaMatcher::Entity

Attributes

attributes[R]

Public Class Methods

new(&blk) click to toggle source
# File lib/schema_matcher/entity.rb, line 5
def initialize(&blk)
  @attributes = {}
  instance_exec(&blk)
end

Public Instance Methods

attribute(name, options = {}, &blk) click to toggle source
# File lib/schema_matcher/entity.rb, line 10
def attribute(name, options = {}, &blk)
  return attributes[name] = { type: self.class.new(&blk) }.merge(options).compact if block_given?

  attributes[name] = { type: :string }.merge(options).compact
end
to_schema() click to toggle source
# File lib/schema_matcher/entity.rb, line 16
def to_schema
  {
    type: :object,
    properties: attributes.transform_values { |attribute| swaggerize_attribute(attribute) }.compact
  }
end

Private Instance Methods

attr_to_schema(attribute) click to toggle source
# File lib/schema_matcher/entity.rb, line 36
def attr_to_schema(attribute)
  return { '$ref' => "#/definitions/#{attribute[:ref]}" } if attribute[:ref]

  if attribute[:type].respond_to?(:to_schema)
    attribute[:type].to_schema
  else
    attribute.except(:nullable, :optional, :array)
  end
end
extra_schema_properties(attribute) click to toggle source
# File lib/schema_matcher/entity.rb, line 46
def extra_schema_properties(attribute)
  {
    'x-nullable' => attribute[:nullable],
    required: attribute[:ref] && !attribute[:array] ? nil : !attribute[:optional]
  }.compact
end
swaggerize_attribute(attribute) click to toggle source
# File lib/schema_matcher/entity.rb, line 25
def swaggerize_attribute(attribute)
  if attribute[:array]
    {
      type: :array,
      item: attr_to_schema(attribute)
    }.merge(extra_schema_properties(attribute))
  else
    attr_to_schema(attribute).merge(extra_schema_properties(attribute))
  end
end