Project

dotpath

0.0
No commit activity in last 3 years
No release in over 3 years
dotpath gives you utilities to navigate and modify hashes and arrays in Ruby with the JSON path to each value in the hash or array.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

= 2.1.4
= 11.0.1
= 5.13.0
= 10.5.0
= 0.78.0
= 0.9.24
 Project Readme

dotpath

dotpath gives you utilities to navigate and modify hashes and arrays in Ruby with the JSON path to each value in the hash or array.

Installation

Add this line to your application's Gemfile:

gem 'dotpath'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install dotpath

Usage

Navigating hashes and arrays

Hash

hash = {
  action: "user.login",
  users: [
    {
      user: "olddewski",
      user_id: "1"
    },
    {
      user: "dewski",
      user_id: "2"
    },
  ],
}

hash.each_with_json_path do |path, value|
  p [path, value]
end

# [".action", "user.login"]
# [".users[0].user", "olddewski"]
# [".users[0].user_id", "1"]
# [".users[1].user", "dewski"]
# [".users[1].user_id", "2"]

Array

array = [
  {
    action: "user.login",
    users: [
      {
        user: "olddewski",
        user_id: "1"
      },
      {
        user: "dewski",
        user_id: "2"
      },
    ],
  },
]

array.each_with_json_path do |path, value|
  p [path, value]
end

# [".[0].action", "user.login"]
# [".[0].users[0].user", "olddewski"]
# [".[0].users[0].user_id", "1"]
# [".[0].users[1].user", "dewski"]
# [".[0].users[1].user_id", "2"]

Selecting values from hashes and arrays using JSON paths

Hash

hash = {
  action: "user.login",
  users: [
    {
      user: "olddewski",
      user_id: "1"
    },
    {
      user: "dewski",
      user_id: "2"
    },
  ],
}

hash.value_at_path(".action") # => "user.login"
hash.value_at_path(".users[0].user") # => "olddewski"
hash.value_at_path(".users[0].user_id") # => "1"
hash.value_at_path(".users[1].user") # => "dewski"
hash.value_at_path(".users[1].user_id") # => "2"

Array

array = [
  {
    action: "user.login",
    users: [
      {
        user: "olddewski",
        user_id: "1"
      },
      {
        user: "dewski",
        user_id: "2"
      },
    ],
  },
]

array.value_at_path(".[0].action") # => "user.login"
array.value_at_path(".[0].users[0].user") # => "olddewski"
array.value_at_path(".[0].users[0].user_id") # => "1"
array.value_at_path(".[0].users[1].user") # => "dewski"
array.value_at_path(".[0].users[1].user_id") # => "2"

Mutating hashes and arrays

hash = {
  action: "user.login",
  users: [
    {
      user: "olddewski",
      user_id: "1"
    },
    {
      user: "dewski",
      user_id: "2"
    },
  ],
}

hash.collect_with_json_path do |path, value|
  if path == ".action"
    value.gsub('.', '_')
  else
    value
  end
end

pp hash

# {
#   action: "user_login",
#   users: [
#     {
#       user: "olddewski",
#       user_id: "1"
#     },
#     {
#       user: "dewski",
#       user_id: "2"
#     },
#   ],
# }

Array

array = [
  {
    action: "user.login",
    users: [
      {
        user: "olddewski",
        user_id: "1"
      },
      {
        user: "dewski",
        user_id: "2"
      },
    ],
  },
]

array.collect_with_json_path do |path, value|
  if path == ".action"
    value.gsub('.', '_')
  else
    value
  end
end

pp array

# [
#   {
#     action: "user_login",
#     users: [
#       {
#         user: "olddewski",
#         user_id: "1"
#       },
#       {
#         user: "dewski",
#         user_id: "2"
#       },
#     ],
#   },
# ]

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/dewski/dotpath. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.

License

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

Code of Conduct

Everyone interacting in the dotpath project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.