Giter Club home page Giter Club logo

hash-path's Introduction

hash-path
=========

  path accessor to hierarchical hash


Class
=====

  * HashPath
      .path    : define path accessor
      .paths   : defined paths


Example
=======

  # JSON.parse(...) returns like this
  hash = {
    "Name" => "foo",
    "Body" => "content",
    "Additional" => {
      "Code" => 1234,
      "StartDateTime"=>"2010/01/10 19:30"
    }
  }

  class Item < HashPath
    path :name, "Name"
    path :body, "Body"
    path :code, "Additional/Code"
    path :time, "Additional/StartDateTime"

    def time
      Time.parse(super)  # parse is defined in active_support, night-time,... gem
    end    
  end 

  item = Item.new(hash)
  item.name  # => "foo"
  item.code  # => 1234
  item.time  # => Sun Jan 10 19:30:00 +0900 2010


Advanced
========

  # Extends HashPath to accept jsonpath syntax for path value.
  # Install gem first, and then require 'hash-path/json'.
  #
  #   gem install jsonpath

  require 'hash-path/json'

  hash = {
    "name"=>"Buono",
    "members"=>
      [{"name"=>"momo", "age"=>17},
       {"name"=>"miya", "age"=>17},
       {"name"=>"airi", "age"=>15}]
  }

  class Group < HashPath
    path :leader              , "$..members[0]"
    path :middle_school_member, "$..members[?(@['age'] <= 15)]"
  end

  group = Group.new(hash)
  group.leader                # => [{"name"=>"momo", "age"=>17}]
  group.middle_school_member  # => [{"name"=>"airi", "age"=>15}]


Todo
====

  * convert value to some type automatically


Author
======

  [email protected]

hash-path's People

Contributors

maiha 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.