Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 31 additions & 46 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,72 +2,57 @@

[![Gem Version](https://badge.fury.io/rb/nested_objects.svg)](https://badge.fury.io/rb/nested_objects)
[![Build Status](https://github.com/main-branch/nested_objects/actions/workflows/continuous_integration.yml/badge.svg)](https://github.com/main-branch/nested_objects/actions/workflows/continuous_integration.yml)
[![Documentation](https://img.shields.io/badge/Documentation-Latest-green)](https://rubydoc.info/gems/nested_objects/)
[![Documentation](https://img.shields.io/badge/Documentation-Latest-green)](https://gemdocs.org/gems/nested_objects)
[![Change Log](https://img.shields.io/badge/CHANGELOG-Latest-green)](https://rubydoc.info/gems/nested_objects/file/CHANGELOG.md)
[![Slack](https://img.shields.io/badge/slack-main--branch/track__open__instances-yellow.svg?logo=slack)](https://main-branch.slack.com/archives/C01CHR7TMM2)

The `NestedObjects` module provides module level methods to safely navigate and
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.

The key methods are:
## Usage

* `NestedObjects.deep_copy(data)` - returns a deep copy of data including nested hash
values and array elements
* `NestedObjects.dig(data, path)` - returns the value at the given path
* `NestedObjects.bury(data, path, value)` - sets a value within the data structure at
the given path
* `NestedObjects.delete(data, path)` - deletes the Hash key or Array index at the
given path
* `NestedObjects.path?(data, path)` - returns true if the path exists in the given
data structure
### Module Methods

These methods (prefixed with `nested_` to avoid method conflicts) can be mixed into
`Object` for ease of use:
These methods are exposted on the `NestedObjects` module. The key methods are:
`deep_copy`, `dig`, `bury`, `delete`, and `path?`.

```Ruby
Object.include NestedObjects::Mixin
Here is an example of using the `dig` method:

data = { 'users' => [{ 'name' => 'John Smith'}, { 'name' => 'Jane Doe' }] }
```Ruby
require 'nested_objects'

data.nested_dig(%w[users 1 name]) #=> 'Jane Doe'
data = { 'people' => [{ 'name' => 'John'}, { 'name', 'Jane' }] }
path = 'people/0/name'.split('/')
NestedObjects.dig(data, path) #=> 'John'
```

If the path is malformed or does not exist, a `BadPathError` will be raised.

## 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.
See documentation and examples of the full API in
[the gem's YARD documentation](https://gemdocs.org/gems/nested_objects/).

## Contributing

Bug reports and pull requests are welcome on GitHub at <https://github.com/main-branch/nested_objects>.
### Object Mixin

### Commit message guidelines
As a convenience, these methods can be mixed into other classes by including the `NestedObjects::Mixin` module.

All commit messages must follow the [Conventional Commits
standard](https://www.conventionalcommits.org/en/v1.0.0/). This helps us maintain a
clear and structured commit history, automate versioning, and generate changelogs
effectively.
In order to reduce the possibility of method name conflicts, all methods are prefixed with `nested_`.

To ensure compliance, this project includes:
```Ruby
Object.include NestedObjects::Mixin

- A git commit-msg hook that validates your commit messages before they are accepted.
data = { 'people' => [{ 'name' => 'John'}, { 'name', 'Jane' }] }
path = 'people/0/name'.split('/')
data.nested_dig(path) #=> 'John'
```

To activate the hook, you must have node installed and run `npm install`.
## Development

- A GitHub Actions workflow that will enforce the Conventional Commit standard as
part of the continuous integration pipeline.
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.

Any commit message that does not conform to the Conventional Commits standard will
cause the workflow to fail and not allow the PR to be merged.
For experimentation, you can also run `bin/console` for an interactive (IRB) prompt
that automatically requires nested_objects.

### Pull request guidelines
## Contributing

All pull requests must be merged using rebase merges. This ensures that commit
messages from the feature branch are preserved in the release branch, keeping the
history clean and meaningful.
See [the contributing guildlines](CONTRIBUTING.md) for guidance on how to contriute
to this project.
4 changes: 2 additions & 2 deletions nested_objects.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ Gem::Specification.new do |spec|
spec.homepage = "https://github.com/main-branch/#{spec.name}"
spec.metadata['homepage_uri'] = spec.homepage
spec.metadata['source_code_uri'] = spec.homepage
spec.metadata['documentation_uri'] = "https://rubydoc.info/gems/#{spec.name}/#{spec.version}"
spec.metadata['changelog_uri'] = "https://rubydoc.info/gems/#{spec.name}/#{spec.version}/file/CHANGELOG.md"
spec.metadata['documentation_uri'] = "https://gemdocs.org/#{spec.name}/#{spec.version}"
spec.metadata['changelog_uri'] = "https://gemdocs.org/#{spec.name}/#{spec.version}/file.CHANGELOG.md"

# Specify which files should be added to the gem when it is released.
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
Expand Down
Loading