Module: Hashie::Extensions::DeepFetch

### Home Classes Methods

### In Files

- lib/hashie/extensions/deep_fetch.rb

### Namespace

- CLASS Hashie::Extensions::DeepFetch::UndefinedPathError

### Methods

- #deep_fetch

### Class/Module Index

Quicksearch

- Hashie
- Hashie::Clash
- Hashie::Clash::ChainError
- Hashie::Dash
- Hashie::Extensions
- Hashie::Extensions::Coercion
- Hashie::Extensions::Coercion::ClassMethods
- Hashie::Extensions::Coercion::InstanceMethods
- Hashie::Extensions::DeepFetch
- Hashie::Extensions::DeepFetch::UndefinedPathError
- Hashie::Extensions::DeepMerge
- Hashie::Extensions::IgnoreUndeclared
- Hashie::Extensions::IndifferentAccess
- Hashie::Extensions::KeyConversion
- Hashie::Extensions::MergeInitializer
- Hashie::Extensions::MethodAccess
- Hashie::Extensions::MethodQuery
- Hashie::Extensions::MethodReader
- Hashie::Extensions::MethodWriter
- Hashie::Extensions::StringifyKeys
- Hashie::Extensions::SymbolizeKeys
- Hashie::Hash
- Hashie::HashExtensions
- Hashie::Mash
- Hashie::PrettyInspect
- Hashie::Rash
- Hashie::Trash

No matching classes.

# Hashie::Extensions::DeepFetch

Searches a deeply nested datastructure for a key path, and returns the associated value.

    options = { user: { location: { address: '123 Street' } } }
    options.deep_fetch :user, :location, :address #=> '123 Street'

If a block is provided its value will be returned if the key does not exist.

    options.deep_fetch(:user, :non_existent_key) { 'a value' } #=> 'a value'

This is particularly useful for fetching values from deeply nested api responses or params hashes.

### Public Instance Methods

deep_fetch(*args, &block) click to toggle source

    # File lib/hashie/extensions/deep_fetch.rb, line 16
    def deep_fetch(*args, &block)
      args.reduce(self) do |obj, arg|
        begin
          arg = Integer(arg) if obj.kind_of? Array
          obj.fetch(arg)
        rescue ArgumentError, IndexError => e
          break block.call(arg) if block
          raise UndefinedPathError, "Could not fetch path (#{args.join(' > ')}) at #{arg}", e.backtrace
        end
      end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.