Class: SchemaTest::Property::Object

Inherits:
SchemaTest::Property show all
Defined in:
lib/schema_test/property.rb

Constant Summary collapse

SHORTHAND_ATTRIBUTES =
{
  id: :integer,
  slug: :string,
  updated_at: :datetime,
  created_at: :datetime
}
TYPES =
{
  boolean: SchemaTest::Property::Boolean,
  integer: SchemaTest::Property::Integer,
  float: SchemaTest::Property::Float,
  string: SchemaTest::Property::String,
  datetime: SchemaTest::Property::DateTime,
  url: SchemaTest::Property::Uri,
  html: SchemaTest::Property::String
}

Instance Attribute Summary collapse

Attributes inherited from SchemaTest::Property

#description, #name, #optional, #type

Instance Method Summary collapse

Methods inherited from SchemaTest::Property

#json_schema_format, #optional!, #optional?

Constructor Details

#initialize(name, description: nil, version: nil, from: nil, properties: nil, &block) ⇒ Object

Returns a new instance of Object.



101
102
103
104
105
106
107
108
109
# 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

Instance Attribute Details

#versionObject (readonly)

Returns the value of attribute version.



99
100
101
# File 'lib/schema_test/property.rb', line 99

def version
  @version
end

Instance Method Details

#==(other) ⇒ Object



116
117
118
# 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) ⇒ Object



159
160
161
# 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) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# 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) ⇒ Object



188
189
190
191
192
193
194
# 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_typeObject



196
197
198
# File 'lib/schema_test/property.rb', line 196

def json_schema_type
  'object'
end

#object(name, desc: nil, as: name, version: nil, &block) ⇒ Object



163
164
165
166
167
168
169
170
# 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

#propertiesObject



111
112
113
114
# File 'lib/schema_test/property.rb', line 111

def properties
  resolve
  @properties
end

#resolveObject



120
121
122
123
124
125
126
127
128
129
130
# 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