Sha256: 9cfcd9a4ed773c28c5cabb3091326915ae0d3c2e4e0cea401329a9996c188952

Contents?: true

Size: 1.93 KB

Versions: 20

Compression:

Stored size: 1.93 KB

Contents

module Hashie
  module Extensions
    module DeepFind
      # 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'
      def deep_find(key)
        _deep_find(key)
      end

      alias_method :deep_detect, :deep_find

      # 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']
      def deep_find_all(key)
        matches = _deep_find_all(key)
        matches.empty? ? nil : matches
      end

      alias_method :deep_select, :deep_find_all

      private

      def _deep_find(key, object = self)
        _deep_find_all(key, object).first
      end

      def _deep_find_all(key, object = self, matches = [])
        deep_locate_result = Hashie::Extensions::DeepLocate.deep_locate(key, object).tap do |result|
          result.map! { |element| element[key] }
        end

        matches.concat(deep_locate_result)
      end
    end
  end
end

Version data entries

20 entries across 18 versions & 3 rubygems

Version Path
tdiary-5.0.8 vendor/bundle/gems/hashie-3.5.7/lib/hashie/extensions/deep_find.rb
hashie-3.5.7 lib/hashie/extensions/deep_find.rb
hashie-3.5.6 lib/hashie/extensions/deep_find.rb
tdiary-5.0.5 vendor/bundle/gems/hashie-3.5.5/lib/hashie/extensions/deep_find.rb
tdiary-5.0.5 vendor/bundle/gems/tdiary-5.0.4/vendor/bundle/gems/hashie-3.5.5/lib/hashie/extensions/deep_find.rb
tdiary-5.0.4 vendor/bundle/gems/hashie-3.5.5/lib/hashie/extensions/deep_find.rb
hashie-3.5.5 lib/hashie/extensions/deep_find.rb
hashie-3.5.4 lib/hashie/extensions/deep_find.rb
hashie-3.5.3 lib/hashie/extensions/deep_find.rb
hashie-3.5.2 lib/hashie/extensions/deep_find.rb
hashie-3.5.1 lib/hashie/extensions/deep_find.rb
hashie-3.5.0 lib/hashie/extensions/deep_find.rb
logstash-input-salesforce-3.0.0 vendor/jruby/1.9/gems/hashie-3.4.6/lib/hashie/extensions/deep_find.rb
hashie-3.4.6 lib/hashie/extensions/deep_find.rb
hashie-3.4.5 lib/hashie/extensions/deep_find.rb
hashie-3.4.4 lib/hashie/extensions/deep_find.rb
tdiary-4.2.1 vendor/bundle/ruby/2.3.0/gems/hashie-3.4.3/lib/hashie/extensions/deep_find.rb
tdiary-4.2.1 vendor/bundle/ruby/2.2.0/gems/hashie-3.4.3/lib/hashie/extensions/deep_find.rb
hashie-3.4.3 lib/hashie/extensions/deep_find.rb
hashie-3.4.2 lib/hashie/extensions/deep_find.rb