Class: JsonInference::BaseNode

Inherits:
Object
  • Object
show all
Defined in:
lib/json-inference/base_node.rb

Direct Known Subclasses

Node, NthChildNode, RootNode

Instance Method Summary collapse

Constructor Details

#initializeBaseNode

Returns a new instance of BaseNode.



3
4
5
6
7
8
9
10
11
12
13
# File 'lib/json-inference/base_node.rb', line 3

def initialize
  @values = NodeValuesCollection.new
  @sub_nodes = Hash.new { |h,k| 
    if k == :nth_child
      sub_node = JsonInference::NthChildNode.new(self)
    else
      sub_node = JsonInference::Node.new(k, self)
    end
    h[k] = sub_node
  }
end

Instance Method Details

#<<(value) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/json-inference/base_node.rb', line 15

def <<(value)
  if value.is_a?(Hash)
    value.each do |key, sub_value|
      @sub_nodes[key] << sub_value
    end
  elsif value.is_a?(Array)
    @sub_nodes[:nth_child]
    value.each do |sub_value|
      @sub_nodes[:nth_child] << sub_value
    end
  end
  @values << value
end

#each_sub_nodeObject



29
30
31
32
33
34
# File 'lib/json-inference/base_node.rb', line 29

def each_sub_node
  @sub_nodes.keys.sort.each do |key|
    sub_node = @sub_nodes[key]
    yield sub_node
  end
end

#indentObject



40
41
42
# File 'lib/json-inference/base_node.rb', line 40

def indent
  '  ' * indent_level
end

#indent_levelObject



36
37
38
# File 'lib/json-inference/base_node.rb', line 36

def indent_level
  @parent.indent_level + 1
end