Module: Hashie::Extensions::DeepFind

Defined in:
lib/hashie/extensions/deep_find.rb

Instance Method Summary collapse

Instance Method Details

#deep_find(key) ⇒ Object Also known as: deep_detect

Performs a depth-first search on deeply nested data structures for a key and returns the first occurrence of the key.

options = {location: {address: '123 Street'}} options.extend(Hashie::Extensions::DeepFind) options.deep_find(:address) # => '123 Street'

class MyHash < Hash include Hashie::Extensions::DeepFind end

my_hash = MyHash.new my_hash[:user] = {address: '123 Street'} my_hash.deep_find(:address) # => '123 Street'



19
20
21
# File 'lib/hashie/extensions/deep_find.rb', line 19

def deep_find(key)
  _deep_find(key)
end

#deep_find_all(key) ⇒ Object Also known as: deep_select

Performs a depth-first search on deeply nested data structures for a key and returns all occurrences of the key.

options = { users: [ { location: '123 Street' }, { location: '234 Street'} ] } options.extend(Hashie::Extensions::DeepFind) options.deep_find_all(:address) # => ['123 Street', '234 Street']

class MyHash < Hash include Hashie::Extensions::DeepFind end

my_hash = MyHash.new my_hash[:users] = [ {address: '123 Street'}, {address: '234 Street'} ] my_hash.deep_find_all(:address) # => ['123 Street', '234 Street']



47
48
49
50
# File 'lib/hashie/extensions/deep_find.rb', line 47

def deep_find_all(key)
  matches = _deep_find_all(key)
  matches.empty? ? nil : matches
end