Class: SchemaTest::Definition

Inherits:
Property::Object show all
Defined in:
lib/schema_test/definition.rb

Constant Summary

Constants inherited from Property::Object

Property::Object::SHORTHAND_ATTRIBUTES, Property::Object::TYPES

Instance Attribute Summary

Attributes inherited from Property::Object

#version

Attributes inherited from Property

#description, #name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Property::Object

#==, #array, #json_schema_type, #object, #properties, #resolve

Methods inherited from Property

#==, #json_schema_format, #json_schema_type, #optional!, #optional?

Constructor Details

#initialize(*args) ⇒ Definition

Returns a new instance of Definition.



25
26
27
28
# File 'lib/schema_test/definition.rb', line 25

def initialize(*args)
  super
  self.class.register(self)
end

Class Method Details

.find(name, version) ⇒ Object



15
16
17
# File 'lib/schema_test/definition.rb', line 15

def self.find(name, version)
  (@definitions || {}).dig(name, version)
end

.find!(name, version) ⇒ Object



19
20
21
22
23
# File 'lib/schema_test/definition.rb', line 19

def self.find!(name, version)
  found = find(name, version)
  raise "Could not find schema for #{name.inspect} (version: #{version.inspect})" unless found
  found
end

.register(definition) ⇒ Object



9
10
11
12
13
# File 'lib/schema_test/definition.rb', line 9

def self.register(definition)
  @definitions ||= {}
  @definitions[definition.name] ||= {}
  @definitions[definition.name][definition.version] = definition
end

.reset!Object



5
6
7
# File 'lib/schema_test/definition.rb', line 5

def self.reset!
  @definitions = nil
end

Instance Method Details

#as_json_schema(domain: SchemaTest.configuration.domain) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/schema_test/definition.rb', line 43

def as_json_schema(domain: SchemaTest.configuration.domain)
  id_part = version ? "v#{version}/#{name}" : name
  {
    '$schema' => SchemaTest::SCHEMA_VERSION,
    '$id' => "http://#{domain}/#{id_part}.json",
    'title' => name.to_s
  }.merge(super(false))
end

#as_structure(_ = nil) ⇒ Object



38
39
40
41
# File 'lib/schema_test/definition.rb', line 38

def as_structure(_=nil)
  hashes, others = @properties.values.map(&:as_structure).partition { |x| x.is_a?(Hash) }
  others + [hashes.inject(&:merge)].compact
end

#based_on(name, version: self.version) ⇒ Object



52
53
54
55
56
57
# File 'lib/schema_test/definition.rb', line 52

def based_on(name, version: self.version)
  other_version = self.class.find(name, version)
  other_version.properties.values.each do |property|
    define_property(property.dup)
  end
end

#optional(object) ⇒ Object



34
35
36
# File 'lib/schema_test/definition.rb', line 34

def optional(object)
  object.optional!
end

#type(name, version = nil) ⇒ Object



30
31
32
# File 'lib/schema_test/definition.rb', line 30

def type(name, version=nil)
  lookup_object(name, version || @version)
end