Module: NxtSchema::Node::HasSubNodes

Included in:
Collection, Constructor, Schema
Defined in:
lib/nxt_schema/node/has_subnodes.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#template_storeObject

Returns the value of attribute template_store.



9
10
11
# File 'lib/nxt_schema/node/has_subnodes.rb', line 9

def template_store
  @template_store
end

#value_storeObject

Returns the value of attribute value_store.



9
10
11
# File 'lib/nxt_schema/node/has_subnodes.rb', line 9

def value_store
  @value_store
end

Instance Method Details

#dupObject



75
76
77
78
79
80
# File 'lib/nxt_schema/node/has_subnodes.rb', line 75

def dup
  result = super
  result.template_store = template_store.deep_dup
  result.options = options.deep_dup
  result
end

#node(name, type_or_node, **options, &block) ⇒ Object

TODO: Would be cool if we could register custom node types!

Raises:

  • (KeyError)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/nxt_schema/node/has_subnodes.rb', line 12

def node(name, type_or_node, **options, &block)
  child_node = case type_or_node.to_s.to_sym
  when :Schema
    NxtSchema::Node::Schema.new(name: name, type: NxtSchema::Types::Strict::Hash, parent_node: self, **options, &block)
  when :Collection
    NxtSchema::Node::Collection.new(name: name, type: NxtSchema::Types::Strict::Array, parent_node: self, **options, &block)
  when :Struct
    NxtSchema::Node::Constructor.new(
      name: name,
      type: NxtSchema::Types::Constructor(::Struct) { |hash| ::Struct.new(*hash.keys).new(*hash.values) },
      parent_node: self,
      **options,
      &block
    )
  when :OpenStruct
    NxtSchema::Node::Constructor.new(
      name: name,
      type: NxtSchema::Types::Constructor(::OpenStruct),
      parent_node: self,
      **options,
      &block
    )
  else
    if type_or_node.is_a?(NxtSchema::Node::Base)
      node = type_or_node.clone
      node.options.merge!(options)
      node.name = name
      node.parent_node = self
      node
    else
      NxtSchema::Node::Leaf.new(name: name, type: type_or_node, parent_node: self, **options)
    end
  end

  # TODO: Should we check if there is a
  raise KeyError, "Duplicate registration for key: #{name}" if template_store.key?(name)
  template_store.push(child_node)

  child_node
end

#nodes(name, **options, &block) ⇒ Object Also known as: array



59
60
61
# File 'lib/nxt_schema/node/has_subnodes.rb', line 59

def nodes(name, **options, &block)
  node(name, :Collection, options, &block)
end

#required(name, type, **options, &block) ⇒ Object Also known as: requires



53
54
55
# File 'lib/nxt_schema/node/has_subnodes.rb', line 53

def required(name, type, **options, &block)
  node(name, type, options, &block)
end

#schema(name, **options, &block) ⇒ Object Also known as: hash



65
66
67
# File 'lib/nxt_schema/node/has_subnodes.rb', line 65

def schema(name, **options, &block)
  node(name, :Schema, options, &block)
end

#struct(name, **options, &block) ⇒ Object



71
72
73
# File 'lib/nxt_schema/node/has_subnodes.rb', line 71

def struct(name, **options, &block)
  node(name, NxtSchema::Types::Constructor(::OpenStruct), options, &block)
end