module Hashie::Extensions::DeepFind
def _deep_find(key, object = self)
def _deep_find(key, object = self) _deep_find_all(key, object).first end
def _deep_find_all(key, object = self, matches = [])
def _deep_find_all(key, object = self, matches = []) deep_locate_result = DeepLocate.deep_locate(key, object).tap do |result| result.map! { |element| element[key] } end matches.concat(deep_locate_result) end
def deep_find(key)
my_hash[:user] = {location: {address: '123 Street'}}
my_hash = MyHash.new
end
include Hashie::Extensions::DeepFind
class MyHash < Hash
options.deep_find(:address) # => '123 Street'
options.extend(Hashie::Extensions::DeepFind)
options = {user: {location: {address: '123 Street'}}}
a key and returns the first occurrence of the key.
Performs a depth-first search on deeply nested data structures for
def deep_find(key) _deep_find(key) end
def deep_find_all(key)
]
{location: {address: '234 Street'}}
{location: {address: '123 Street'}},
my_hash[:users] = [
my_hash = MyHash.new
end
include Hashie::Extensions::DeepFind
class MyHash < Hash
options.deep_find_all(:address) # => ['123 Street', '234 Street']
options.extend(Hashie::Extensions::DeepFind)
}
]
{ location: {address: '234 Street'}}
{ location: {address: '123 Street'} },
users: [
options = {
a key and returns all occurrences of the key.
Performs a depth-first search on deeply nested data structures for
def deep_find_all(key) matches = _deep_find_all(key) matches.empty? ? nil : matches end