Sha256: 8eeb27e011f89d88d41b726bdf6458ea28c16b1a57a378b35fd5ac6a897b3add

Contents?: true

Size: 829 Bytes

Versions: 4

Compression:

Stored size: 829 Bytes

Contents

require 'active_support/core_ext/hash/indifferent_access'

class HashWithIndifferentAccess

  # Perform a depth first search of this hash and return the first element
  # matching +path+, or +default+ if nothing found.
  # @param [Array[String | Symbol] | String | Symbol] path to the element of interest.
  # @param [Object] default the object to be returned if there is no result for +path+.
  # @return [Object] the first object found on +path+ or +default+.
  #
  def deep_find(path, default = nil)
    return default unless path
    path = [path] if path.is_a?(String) || path.is_a?(Symbol)
    return default unless path.is_a?(Array) && !path.empty?

    location = self
    path.each do |key|
      return default if location.nil? || !location.key?(key)
      location = location[key]
    end
    return location
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ebay-trader-0.9.8 lib/hash_with_indifferent_access.rb
ebay-trader-0.9.7 lib/hash_with_indifferent_access.rb
ebay-trader-0.9.6 lib/hash_with_indifferent_access.rb
ebay-trader-0.9.5 lib/hash_with_indifferent_access.rb