Module: KeyPath::HashExtensions
- Included in:
- Hash
- Defined in:
- lib/key_path/hash/extensions.rb
Overview
A Mixin for Hash which allows us to create a path from a key.
Instance Method Summary collapse
Instance Method Details
#keypaths_for_nested_key(nested_key = '', nested_hash = self, path = [], all_values = {}) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/key_path/hash/extensions.rb', line 4 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 all_values.merge!("#{path.join('.')}" => "#{v}") if k == nested_key path.pop end end path.pop all_values end |