Module: KeyPath::HashExtensions

Included in:
Hash
Defined in:
lib/key_path/hash/extensions.rb

Instance Method Summary collapse

Instance Method Details

#keypaths_for_nested_key(nested_key = '', nested_hash = self, path = [], all_values = {}) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/key_path/hash/extensions.rb', line 3

def keypaths_for_nested_key(nested_key = '', nested_hash=self, path=[], all_values={})
  nested_hash.each do |k, v|
    path << k.to_s # assemble the path from the key
    case v
    when Array then
      v.each_with_index do |item, i|
        path << "#{i}" # add the array key
        keypaths_for_nested_key(nested_key, item, path, all_values)
      end
      path.pop # remove the array key
    when Hash then keypaths_for_nested_key(nested_key, v, path, all_values)
    else
      if k == nested_key
        all_values.merge!({"#{path.join('.')}" => "#{v}"})
      end
      path.pop
    end
  end
  path.pop
   
  return all_values
end