Module: NxtSchema::Template::HasSubNodes

Included in:
AnyOf, Collection, Schema
Defined in:
lib/nxt_schema/template/has_sub_nodes.rb

Instance Method Summary collapse

Instance Method Details

#[](key) ⇒ Object



76
77
78
# File 'lib/nxt_schema/template/has_sub_nodes.rb', line 76

def [](key)
  sub_nodes[key]
end

#add_sub_node(node) ⇒ Object



67
68
69
70
# File 'lib/nxt_schema/template/has_sub_nodes.rb', line 67

def add_sub_node(node)
  sub_nodes.add(node)
  node
end

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



30
31
32
33
34
35
36
37
38
39
# File 'lib/nxt_schema/template/has_sub_nodes.rb', line 30

def any_of(name, **options, &block)
  node = NxtSchema::Template::AnyOf.new(
    name: name,
    parent_node: self,
    **options,
    &block
  )

  add_sub_node(node)
end

#collection(name, type = NxtSchema::Template::Collection::DEFAULT_TYPE, **options, &block) ⇒ Object Also known as: nodes



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/nxt_schema/template/has_sub_nodes.rb', line 4

def collection(name, type = NxtSchema::Template::Collection::DEFAULT_TYPE, **options, &block)
  node = NxtSchema::Template::Collection.new(
    name: name,
    type: type,
    parent_node: self,
    **options,
    &block
  )

  add_sub_node(node)
end

#ensure_sub_nodes_presentObject



80
81
82
83
84
# File 'lib/nxt_schema/template/has_sub_nodes.rb', line 80

def ensure_sub_nodes_present
  return if sub_nodes.any?

  raise NxtSchema::Errors::InvalidOptions, "#{self.class.name} must have sub nodes"
end

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



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/nxt_schema/template/has_sub_nodes.rb', line 41

def node(name, node_or_type_of_node, **options, &block)
  node = if node_or_type_of_node.is_a?(NxtSchema::Template::Base)
    raise ArgumentError, "Can't provide a block along with a node" if block.present?

    node_or_type_of_node.class.new(
      name: name,
      type: node_or_type_of_node.type,
      parent_node: self,
      **node_or_type_of_node.options.merge(options),
      &node_or_type_of_node.configuration
    )
  else
    NxtSchema::Template::Leaf.new(
      name: name,
      type: node_or_type_of_node,
      parent_node: self,
      **options,
      &block
    )
  end

  add_sub_node(node)
end

#schema(name, type = NxtSchema::Template::Schema::DEFAULT_TYPE, **options, &block) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/nxt_schema/template/has_sub_nodes.rb', line 18

def schema(name, type = NxtSchema::Template::Schema::DEFAULT_TYPE, **options, &block)
  node = NxtSchema::Template::Schema.new(
    name: name,
    type: type,
    parent_node: self,
    **options,
    &block
  )

  add_sub_node(node)
end

#sub_nodesObject



72
73
74
# File 'lib/nxt_schema/template/has_sub_nodes.rb', line 72

def sub_nodes
  @sub_nodes ||= Template::SubNodes.new
end