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

Constants inherited from Property

Property::NULL_TYPE

Instance Attribute Summary collapse

Attributes inherited from Property::Object

#excluded_property_names, #version

Attributes inherited from Property

#_type, #description, #name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Property::Object

#==, #array, #base_json_schema_type, #based_on, #object, #properties, #resolve

Methods inherited from Property

#==, #base_json_schema_type, #json_schema_format, #json_schema_type, #lookup_object, #nullable, #nullable!, #nullable?, #optional, #optional!, #optional?, #type

Constructor Details

#initialize(name, location: nil, **attributes) ⇒ Definition

Returns a new instance of Definition.



31
32
33
34
35
# File 'lib/schema_test/definition.rb', line 31

def initialize(name, location: nil, **attributes)
  super(name, **attributes)
  self.class.register(self)
  @location = location
end

Instance Attribute Details

#locationObject (readonly)

Returns the value of attribute location.



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

def location
  @location
end

Class Method Details

.allObject



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

def self.all
  (@definitions || {}).values.flat_map(&:values)
end

.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

Raises:



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

def self.find!(name, version)
  found = find(name, version)
  raise SchemaTest::Error, "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



37
38
39
40
41
42
43
44
# File 'lib/schema_test/definition.rb', line 37

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