Class: Schemurai::Internal::SchemaNode

Inherits:
Object
  • Object
show all
Defined in:
lib/schemurai/schema_node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema:, dialect:, base_uri:, schema_path:, resource_path:, document_key:) ⇒ SchemaNode

Returns a new instance of SchemaNode.



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/schemurai/schema_node.rb', line 14

def initialize(schema:, dialect:, base_uri:, schema_path:, resource_path:, document_key:)
  @schema = schema
  @dialect = dialect
  @base_uri = base_uri
  @schema_path = schema_path
  @resource_path = resource_path
  @document_key = document_key
  @keyword_mask = schema.is_a?(Hash) ? dialect.keyword_mask(schema) : 0
  @format = Formats.resolve(schema["format"]) if schema.is_a?(Hash) && schema.key?("format")
  @children = nil
end

Instance Attribute Details

#base_uriObject (readonly)

Returns the value of attribute base_uri.



10
11
12
# File 'lib/schemurai/schema_node.rb', line 10

def base_uri
  @base_uri
end

#dialectObject (readonly)

Returns the value of attribute dialect.



10
11
12
# File 'lib/schemurai/schema_node.rb', line 10

def dialect
  @dialect
end

#document_keyObject (readonly)

Returns the value of attribute document_key.



10
11
12
# File 'lib/schemurai/schema_node.rb', line 10

def document_key
  @document_key
end

#formatObject (readonly)

Returns the value of attribute format.



10
11
12
# File 'lib/schemurai/schema_node.rb', line 10

def format
  @format
end

#keyword_maskObject (readonly)

Returns the value of attribute keyword_mask.



10
11
12
# File 'lib/schemurai/schema_node.rb', line 10

def keyword_mask
  @keyword_mask
end

#resourceObject

Returns the value of attribute resource.



12
13
14
# File 'lib/schemurai/schema_node.rb', line 12

def resource
  @resource
end

#resource_pathObject (readonly)

Returns the value of attribute resource_path.



10
11
12
# File 'lib/schemurai/schema_node.rb', line 10

def resource_path
  @resource_path
end

#schemaObject (readonly)

Returns the value of attribute schema.



10
11
12
# File 'lib/schemurai/schema_node.rb', line 10

def schema
  @schema
end

#schema_pathObject (readonly)

Returns the value of attribute schema_path.



10
11
12
# File 'lib/schemurai/schema_node.rb', line 10

def schema_path
  @schema_path
end

Instance Method Details

#add_child(keyword, segment = MISSING_SEGMENT, child:) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/schemurai/schema_node.rb', line 26

def add_child(keyword, segment = MISSING_SEGMENT, child:)
  children = (@children ||= {})
  if segment.equal?(MISSING_SEGMENT)
    children[keyword] = child
  else
    (children[keyword] ||= {})[segment] = child
  end
end

#child(keyword, segment = MISSING_SEGMENT) ⇒ Object



35
36
37
38
39
40
# File 'lib/schemurai/schema_node.rb', line 35

def child(keyword, segment = MISSING_SEGMENT)
  return unless @children
  return @children[keyword] if segment.equal?(MISSING_SEGMENT)

  @children.dig(keyword, segment)
end

#freezeObject



42
43
44
45
46
47
48
# File 'lib/schemurai/schema_node.rb', line 42

def freeze
  if @children
    @children.each_value { |value| value.freeze if value.is_a?(Hash) }
    @children.freeze
  end
  super
end