Coder Social home page Coder Social logo

ruby-tree_filter's Introduction

Tree Filter

Filter arbitrary data trees with a concise query language. Similar to how the Jenkins API works, if you happen to be familiar with that.

name,environments             # Select specific attributes from a hash
environments[id,last_deploy]  # Select attributes from sub-hash
environments[*]               # Select all attributes

Usage

require 'tree_filter'

data = {
  'name' => 'don',
  'contact' => {
    'phone' => '415-123-4567',
    'email' => '[email protected]'
  }
}

TreeFilter.new("name,contact[email]").filter(data)
# => {'name' => 'don', 'contact' => {'email' => '[email protected]'}}

Different data structures can be presented dependent on whether they are explicitly expanded or not. This is typically used when referring to other resources in an API response.

data = {
  'name' => 'don',
  'contact' => TreeFilter::Leaf.new('/contact-data/1', {
    'phone' => '415-123-4567',
    'email' => '[email protected]'
  })
}

TreeFilter.new("*").filter(data)
# => {'name' => 'don', 'contact' => '/contact-data/1'}

TreeFilter.new("contact[*]").filter(data)
# => {'contact' => {'phone' => '415-123-4567', 'email' => '[email protected]'}}

For nested data structures, evaluation can be defered until it is actually required. This can defer resource lookups, and also allows cyclic structures!

data = { 'name' => 'don', }

data['contact'] = TreeFilter::Leaf.new(
  '/contact-data/1',
  TreeFilter::Defer.new(->{{
    'email' => '[email protected]',
    'person' => TreeFilter::Leaf.new('/person/1', data)
  }})
)

TreeFilter.new("contact[person[contact[email]]]").filter(data)
# => {'contact' => {'person' => {'contact' => {'email' => '[email protected]'}}}}

Compatibility

All rubies that ruby core supports! Should work on JRuby and Rubinius too.

Support

Make a new github issue.

Contributing

Fork and patch! Before any changes are merged to master, we need you to sign an Individual Contributor Agreement (Google Form).

Run tests:

gem install bundler
bundle
bundle exec rspec

ruby-tree_filter's People

Contributors

xaviershay avatar

Watchers

Sarvex โ˜  Jatasra 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.