Class: JsonInference::BaseNode

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

Direct Known Subclasses

Node, NthChildNode, RootNode

Instance Method Summary collapse

Constructor Details

#initializeBaseNode

Returns a new instance of BaseNode.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/json-inference.rb', line 7

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

Instance Method Details

#<<(value) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/json-inference.rb', line 19

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
  if value.class == String && value =~ /^(\d){4}-(\d){2}-(\d){2}T(\d){2}:(\d){2}:(\d){2}\.(\d){3}Z$/
    @value_classes[Date] += 1
  elsif [true, false].include?(value)
    @value_classes['Boolean'] += 1
  else
    @value_classes[value.class] += 1
  end
end

#each_sub_nodeObject



39
40
41
42
43
44
# File 'lib/json-inference.rb', line 39

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

#indentObject



50
51
52
# File 'lib/json-inference.rb', line 50

def indent
  '  ' * indent_level
end

#indent_levelObject



46
47
48
# File 'lib/json-inference.rb', line 46

def indent_level
  @parent.indent_level + 1
end

#total_countObject



54
55
56
# File 'lib/json-inference.rb', line 54

def total_count
  @value_classes.values.inject { |sum, i| sum + i } || 0
end