nested_objects gem
The NestedObjects
module provides module methods to safely navigate and
manage a heirarchy of Ruby POROs nested using Hashes or Arrays. Think of these
nested data objects like what you would get after reading in a JSON file.
Usage
Module Methods
These methods are exposted on the NestedObjects
module. The key methods are:
deep_copy
, dig
, bury
, delete
, and path?
.
Here is an example of using the dig
method:
require 'nested_objects'
data = { 'people' => [{ 'name' => 'John'}, { 'name' => 'Jane' }] }
path = 'people/0/name'.split('/')
NestedObjects.dig(data, path) #=> 'John'
See documentation and examples of the full API in the gem's YARD documentation.
Object Mixin
As a convenience, these methods can be mixed into other classes by including the
NestedObjects::Mixin
module.
In order to reduce the possibility of method name conflicts, all methods are prefixed
with nested_
.
require 'nested_objects'
Object.include NestedObjects::Mixin
data = { 'people' => [{ 'name' => 'John'}, { 'name' => 'Jane' }] }
path = 'people/0/name'.split('/')
data.nested_dig(path) #=> 'John'
Development
After checking out the repo, run bin/setup
to install dependencies. Then, run
bundle exec rake
to run tests, static analysis, and build the gem.
For experimentation, you can also run bin/console
for an interactive (IRB) prompt
that automatically requires nested_objects.
Contributing
See the contributing guildlines for guidance on how to contriute to this project.