0.0
No commit activity in last 3 years
No release in over 3 years
Access values hidden in deeply nested data structures with ease and without fear.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

>= 1.2.0
 Project Readme

Nested Value

Finally, a safe way to navigate the mean streets of nested data structures. Add it to your project as a gem dependency and use it like so:

require "nested_value"

class MyClass
  include NestedValue

  def initialize
    @my_data = {:come_out => [{:come_out => {:wherever =>
      ["wrong turn", {:you_are => "Found ME!!!"}]}}, "some_value", 0]}
  end

  def correct
    nested_value(@my_data, :come_out, 0, :come_out, :wherever, 1, :you_are)
  end

  def incorrect
    nested_value(@my_data, :come_out, 0, :wherever, 0, :you_are)
  end

  def not_even_close
    nested_value(@my_data, :where, :are, :you, 99)
  end

  def going_halfway
    nested_value(@my_data, :come_out, 0, :come_out)
  end
end

MyClass.new.correct        #=> "Found Me!!!"
MyClass.new.incorrect      #=> nil
MyClass.new.not_even_close #=> nil
MyClass.new.going_halfway  #=> {:wherever=>["wrong turn", {:you_are=>"Found ME!!!"}]}

You could also extend the NestedValue module if you prefer.

In versions 0.0.2 and above, you can patch nested_value_for on Array and Hash with ease. All you have to do is require the patch_nested_value file.

require "patch_nested_value"

my_array = [5,3,4,5,6,{some: "value"}]
my_array.nested_value_for(5, :some) #=> "value"

my_hash = {I: {can: {has: [:moneyz]}}}
my_hash.nested_value_for(:I, :can, :has, 0) #=> :moneyz

Enjoy!