Skip to content
/ dig-deep Public
forked from hszeto/dig-deep

Dig nested hashes by the key.

License

Notifications You must be signed in to change notification settings

watg/dig-deep

 
 

Repository files navigation

DigDeep

DigDeep will look up key/value pairs in a nested hash by a given key. It will recursively dig each nested object and return all matching key/values.

Ruby's dig method can perform similar tasks, but you need to specify the path to the target key.

With this gem, you will only need to specify the target key and it will do the digging.

Installation

Add this line to your application's Gemfile:

gem 'dig-deep'

And then execute:

$ bundle

Or install it yourself as:

$ gem install dig-deep

Test

$ bundle exec rspec spec

Usage

Example 1:

Given the following hash:

data = {
  a: {
    b: {
      c: "abc"
    }
  }
}

Ruby .dig:

data.dig(:a, :b, :c)   // "abc"

With DigDeep:

require 'dig-deep'

data.dig_deep(:c)      // "abc"

Example 2:

Dig up all :email from arrays of objects

data = {
  contacts: [{
    name: "John",
    email: "john@example.com"
  }, {
    name: "Mary",
    email: "mary@example.com"
  }],
  work: {
    contacts: [{
      name: "ACME Corp.",
      email: "acme@example.com"
    }, {
      name: "Asdf Inc.",
      email: "asdf@example.com"
    }]
  }
}
require 'dig-deep'

data.dig_deep(:email)  // ["john@example.com", "mary@example.com", "acme@example.com", "asdf@example.com"]

Example 3:

Object with boolean, string, array...

data = {
  :l1 => {
    :l2 => {
      :l3 => {
        :l4a => "Level 4",
        :l4b => {
          :l5a => false,
          :l5b => {
            :l6 => ["apple", "orange"]
          }
        } 
      }
    }
  },
  :l7 => true,
  :l8 => {
    :l9 => 9876
  }
}
require 'dig-deep'

data.dig_deep(:l4a)   // "Level 4"

data.dig_deep(:l5a)   // false

data.dig_deep(:l6)    // ["apple", "orange"]

data.dig_deep(:l9)    // 9876

data.dig_deep(:xyz)   // nil   (returns nil when key is not found)

Example 4:

Object with symbol and string keys

data = {
  :email  => "email+1@example.com",
  :level1 => {
    "email" => "email+2@example.com"
  }
}
require 'dig-deep'

data.dig_deep(:email)   // ["email+1@example.com", "email+2@example.com"]

data.dig_deep('email')  // ["email+1@example.com", "email+2@example.com"]

Contributing

Bug reports and pull requests are welcome!

Fork it, create your new branch, push and create a pull request.

License

The gem is available as open source under the terms of the MIT License.

About

Dig nested hashes by the key.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Ruby 98.9%
  • Shell 1.1%