Class: JsonToToon::NodeBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/json_to_toon/node_builder.rb

Class Method Summary collapse

Class Method Details

.build(data, parent = nil) ⇒ Object



9
10
11
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
# File 'lib/json_to_toon/node_builder.rb', line 9

def self.build(data, parent = nil)
  case data
  when Hash
    node = ObjectNode.new(parent)
    data.each do |key, value|
      child_node = self.build(value, node)

      node.add_key_value(key, child_node)
    end

    node

  when Array
    node = ArrayNode.new(parent)

    data.each do |item|
      child_node = self.build(item, node)

      node.add_child(child_node)
    end

    node

  when String, Numeric, TrueClass, FalseClass, NilClass
    ValueNode.new(data, parent)
  else
    ValueNode.new(data.to_s, parent)
  end
end