Code Monkey home page Code Monkey logo

keypath-ruby's Introduction

keypath-based collection access extensions for Ruby.

Build Status Code Climate Test Coverage Dependency Status

This gem allows you to access nested Ruby collections (Hash, Array, etc) using keypaths.

For example, say you had a nested data structure like:

data = {
    :item_one => {:id => 1, :url => 'http://nickcharlton.net/'},
    :something_else => [
        {
            :id => 1,
            :url => 'https://github.com/'
        }
    ]
}

You could access "https://github.com/" through: something_else.0.url. Basically, this is intended to allow you to manipulate/transform large nested structures that you might get back from a JSON document.

Installation

keypath-ruby is on RubyGems as key_path. But you can also add the repo to your Gemfile:

gem 'key_path', :git => 'https://github.com/nickcharlton/keypath-ruby.git'

Usage

KeyPath is at least two things. First, it's a class (actually, KeyPath::Path) which represents a path (this is just a string, and has methods to go back and forth from it) and secondly a set of class extensions for Enumerable, Hash and String which allow you to use the native collection classes with keypaths.

require 'key_path'

data = {
    :item => {
        :url => 'http://nickcharlton.net'
    }
}

# fetching a path
path = KeyPath::Path.new 'item.url'
data.value_at_keypath(path) #=> 'http://nickcharlton.net'

# finding all `:url` paths in a collection
data.keypaths_for_nested_key(:url) #=> {item.url => 'http://nickcharlton.net'}

# going back and forth from a string
path.to_s #=> 'item.url'
'item.url'.to_keypath #=> #<KeyPath:70096895112220 path=item.url>

# get the parent of a keypath (or nil, if there isn't one)
path.parent #=> #<KeyPath:70096895221110 path=item>

# setting a path
data.set_keypath(path, 'http://github.com/')

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Author

Copyright (c) 2013 Nick Charlton ([email protected])

keypath-ruby's People

Contributors

nickcharlton avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.