Class: Reynard::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/reynard/schema.rb,
lib/reynard/schema/model_naming.rb

Overview

Holds a references to a schema definition in the specification.

Defined Under Namespace

Classes: ModelNaming

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(specification:, node:, namespace: nil) ⇒ Schema

Returns a new instance of Schema.



10
11
12
13
14
# File 'lib/reynard/schema.rb', line 10

def initialize(specification:, node:, namespace: nil)
  @specification = specification
  @node = node
  @namespace = namespace
end

Instance Attribute Details

#namespaceObject (readonly)

Returns the value of attribute namespace.



8
9
10
# File 'lib/reynard/schema.rb', line 8

def namespace
  @namespace
end

#nodeObject (readonly)

Returns the value of attribute node.



8
9
10
# File 'lib/reynard/schema.rb', line 8

def node
  @node
end

Instance Method Details

#item_schemaObject

Returns the schema for items when the current schema is an array.



27
28
29
30
31
32
33
34
35
# File 'lib/reynard/schema.rb', line 27

def item_schema
  return unless type == 'array'

  self.class.new(
    specification: @specification,
    node: [*node, 'items'],
    namespace: [*namespace, model_name]
  )
end

#model_nameObject



22
23
24
# File 'lib/reynard/schema.rb', line 22

def model_name
  @model_name || model_naming.model_name
end

#property_schema(name) ⇒ Object

Returns the schema for a propery in the schema.



38
39
40
41
42
43
44
45
46
47
# File 'lib/reynard/schema.rb', line 38

def property_schema(name)
  property_node = [*node, 'properties', name.to_s]
  return unless @specification.dig(*property_node)

  self.class.new(
    specification: @specification,
    node: property_node,
    namespace: [*namespace, model_name]
  )
end

#typeObject



16
17
18
19
20
# File 'lib/reynard/schema.rb', line 16

def type
  return @type if defined?(@type)

  @type = @specification.dig(*node, 'type')
end