Module: Codgen::Flattener

Defined in:
lib/codgen/flattener.rb

Class Method Summary collapse

Class Method Details

.merge(data_root, properties) ⇒ Array, Hash

Parameters:

  • data_root: (Hash)

    the JSON root hash

  • properties (Array[String])

Returns:

  • (Array, Hash)


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/codgen/flattener.rb', line 7

def self.merge(data_root, properties)
  current = data_root
  obj_out = Hash.new
  properties.each { |property|
    current.each {|prop, val| obj_out[prop] = val}
    current = current[property]
  }

  if current.is_a?(Hash)
    current.each {|prop, val| obj_out[prop] = val}
    return obj_out
  elsif current.is_a?(Array)
    array_out = Array.new
    current.each { |child|
      if child.is_a?(Hash)
        array_obj = Hash.new
        obj_out.each {|prop, val| array_obj[prop] = val}
        child.each {|prop, val| array_obj[prop] = val}
        array_out.push(array_obj)
      end
    }
    return array_out
  end
end