Class: YamlToCsv::HashFlattener

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(merge_keys:) ⇒ HashFlattener

Returns a new instance of HashFlattener.



57
58
59
# File 'lib/yaml_to_csv.rb', line 57

def initialize(merge_keys:)
  @merge_keys = merge_keys
end

Instance Attribute Details

#merge_keysObject (readonly)

Returns the value of attribute merge_keys.



55
56
57
# File 'lib/yaml_to_csv.rb', line 55

def merge_keys
  @merge_keys
end

Instance Method Details

#flatten(hsh, parent: nil) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/yaml_to_csv.rb', line 61

def flatten(hsh, parent: nil)
  hsh.reduce({}) do |flat, (key, value)|
    new_key = merge_keys.call(parent, key)
    if value.is_a?(Hash)
      flat.merge!(flatten(value, parent: new_key))
    else
      flat[new_key] = value
    end
    flat
  end
end