forked from maiha/hash-path
-
Notifications
You must be signed in to change notification settings - Fork 0
/
README
83 lines (59 loc) · 1.47 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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
======
maiha@wota.jp