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 case v
when Array then
v.each_with_index do |item, i|
path << "#{i}" keypaths_for_nested_key(nested_key, item, path, all_values)
end
path.pop 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
|