module Hashie::Extensions::DeepFind

@example Find values in nested collections

hash = { users: [{ name: 'Ada' }] }.extend(Hashie::Extensions::DeepFind)
hash.deep_find(:name) # => 'Ada'

Public Instance Methods

deep_detect(key)

@example Detect the first nested value

hash = { user: { name: 'Ada' } }.extend(Hashie::Extensions::DeepFind)
hash.deep_detect(:name) # => 'Ada'
Alias for: deep_find
deep_find(key) click to toggle source

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

options = {user: {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] = {location: {address: '123 Street'}}
my_hash.deep_find(:address) # => '123 Street'

@example Return the first value

hash = { users: [{ name: 'Ada' }, { name: 'Grace' }] }.extend(Hashie::Extensions::DeepFind)
hash.deep_find(:name) # => 'Ada'
# File lib/hashie/extensions/deep_find.rb, line 28
def deep_find(key)
  _deep_find(key)
end
Also aliased as: deep_detect
deep_find_all(key) click to toggle source

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

options = {
  users: [
    { location: {address: '123 Street'} },
    { location: {address: '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] = [
  {location: {address: '123 Street'}},
  {location: {address: '234 Street'}}
]
my_hash.deep_find_all(:address) # => ['123 Street', '234 Street']

@example Collect all values, or nil if absent

hash = { users: [{ name: 'Ada' }, { name: 'Grace' }] }.extend(Hashie::Extensions::DeepFind)
hash.deep_find_all(:name) # => ['Ada', 'Grace']
hash.deep_find_all(:email) # => nil
# File lib/hashie/extensions/deep_find.rb, line 64
def deep_find_all(key)
  matches = _deep_find_all(key)
  matches.empty? ? nil : matches
end
Also aliased as: deep_select
deep_select(key)

@example Select every nested value

hash = { users: [{ name: 'Ada' }, { name: 'Grace' }] }.extend(Hashie::Extensions::DeepFind)
hash.deep_select(:name) # => ['Ada', 'Grace']
Alias for: deep_find_all

Private Instance Methods

_deep_find(key, object = self) click to toggle source
# File lib/hashie/extensions/deep_find.rb, line 76
def _deep_find(key, object = self)
  _deep_find_all(key, object).first
end
_deep_find_all(key, object = self, matches = []) click to toggle source
# File lib/hashie/extensions/deep_find.rb, line 80
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