class SchemaTest::Property::SchemaTest::Property::SchemaTest::Property::Object

Constants

SHORTHAND_ATTRIBUTES
TYPES

Attributes

version[R]

Public Class Methods

new(name, description: nil, version: nil, from: nil, properties: nil, &block) click to toggle source
# File lib/schema_test/property.rb, line 101
def initialize(name, description: nil, version: nil, from: nil, properties: nil, &block)
  @name = name
  @description = description
  @version = version
  @specific_properties = properties
  @properties = {}
  @from = from
  instance_eval(&block) if block_given?
end

Public Instance Methods

==(other) click to toggle source
Calls superclass method
# File lib/schema_test/property.rb, line 116
def ==(other)
  super && properties.all? { |name, property| property == other.properties[name] }
end
array(name, of: nil, desc: nil, &block) click to toggle source
# File lib/schema_test/property.rb, line 159
def array(name, of: nil, desc: nil, &block)
  define_property(SchemaTest::Property::Array.new(name, of, desc, &block))
end
as_json_schema(include_root=true) click to toggle source
# File lib/schema_test/property.rb, line 172
def as_json_schema(include_root=true)
  property_values = properties.values
  required_property_names = property_values.reject(&:optional?).map(&:name).map(&:to_s)
  schema = {
    'type' => json_schema_type,
    'properties' => property_values.inject({}) { |a,p| a.merge(p.as_json_schema) },
    'required' => required_property_names,
    'additionalProperties' => false
  }
  if include_root
    { name.to_s => schema }
  else
    schema
  end
end
as_structure(include_root=true) click to toggle source
# File lib/schema_test/property.rb, line 188
def as_structure(include_root=true)
  if include_root
    { name => properties.values.map(&:as_structure) }
  else
    properties.values.map(&:as_structure)
  end
end
json_schema_type() click to toggle source
# File lib/schema_test/property.rb, line 196
def json_schema_type
  'object'
end
object(name, desc: nil, as: name, version: nil, &block) click to toggle source
# File lib/schema_test/property.rb, line 163
def object(name, desc: nil, as: name, version: nil, &block)
  inferred_version = version || @version
  if block_given?
    define_property(SchemaTest::Property::Object.new(as, description: desc, version: inferred_version, &block))
  else
    define_property(SchemaTest::Property::Object.new(as, description: desc, version: inferred_version, from: lookup_object(name, inferred_version)))
  end
end
properties() click to toggle source
# File lib/schema_test/property.rb, line 111
def properties
  resolve
  @properties
end
resolve() click to toggle source
# File lib/schema_test/property.rb, line 120
def resolve
  if @from
    @properties.merge!(@from.properties)
    @from = nil
  end
  if @specific_properties
    @specific_properties.each { |p| define_property(p) }
    @specific_properties = nil
  end
  self
end

Private Instance Methods

define_property(attribute) click to toggle source
# File lib/schema_test/property.rb, line 202
def define_property(attribute)
  @properties[attribute.name] = attribute
end
lookup_object(name, version) click to toggle source
# File lib/schema_test/property.rb, line 206
def lookup_object(name, version)
  UnresolvedProperty.new(name, version: version)
end