hash-path 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README CHANGED
@@ -42,10 +42,37 @@ Example
42
42
  item.time # => Sun Jan 10 19:30:00 +0900 2010
43
43
 
44
44
 
45
+ Advanced
46
+ ========
47
+
48
+ # Extends HashPath to accept jsonpath syntax for path value.
49
+ # Install gem first, and then require 'hash-path/json'.
50
+ #
51
+ # gem install jsonpath
52
+
53
+ require 'hash-path/json'
54
+
55
+ hash = {
56
+ "name"=>"Buono",
57
+ "members"=>
58
+ [{"name"=>"momo", "age"=>17},
59
+ {"name"=>"miya", "age"=>17},
60
+ {"name"=>"airi", "age"=>15}]
61
+ }
62
+
63
+ class Group < HashPath
64
+ path :leader , "$..members[0]"
65
+ path :middle_school_member, "$..members[?(@['age'] <= 15)]"
66
+ end
67
+
68
+ group = Group.new(hash)
69
+ group.leader # => [{"name"=>"momo", "age"=>17}]
70
+ group.middle_school_member # => [{"name"=>"airi", "age"=>15}]
71
+
72
+
45
73
  Todo
46
74
  ====
47
75
 
48
- * create spec
49
76
  * convert value to some type automatically
50
77
 
51
78
 
data/Rakefile CHANGED
@@ -6,7 +6,7 @@ AUTHOR = "maiha"
6
6
  EMAIL = "maiha@wota.jp"
7
7
  HOMEPAGE = "http://github.com/maiha/hash-path"
8
8
  SUMMARY = "path accessor to hierarchical hash"
9
- GEM_VERSION = "0.0.2"
9
+ GEM_VERSION = "0.0.3"
10
10
 
11
11
  spec = Gem::Specification.new do |s|
12
12
  s.rubyforge_project = 'asakusarb'
@@ -1,23 +1,29 @@
1
-
2
1
  class HashPath < Hash
3
- def self.paths
4
- @paths ||= {}
5
- end
2
+ class << self
3
+ def paths
4
+ @paths ||= {}
5
+ end
6
6
 
7
- def self.path(key, path)
8
- paths[key.to_s] = path
7
+ def path(key, path)
8
+ paths[key.to_s] = path
9
+ end
9
10
  end
10
11
 
11
12
  def [](key)
12
13
  key = key.to_s
13
14
 
14
- # first, check exact value
15
- return super if has_key?(key)
15
+ if (key[0,1] == '$') && defined?(JsonPath)
16
+ # jsonpath
17
+ @jsonpath ||= JsonPath.wrap(self)
18
+ return @jsonpath.path(key).to_a
16
19
 
17
- # second, browse children
18
- return key.split("/").inject(self) {|hash, key| hash[key]}
19
- rescue Exception => e
20
- return rescue_hierarchical_access(key, e)
20
+ else
21
+ # first, check if key has '/'
22
+ return super unless key.index('/')
23
+
24
+ # second, browse children
25
+ return key.split("/").inject(self) {|hash, k| hash ? hash[k] : nil}
26
+ end
21
27
  end
22
28
 
23
29
  def initialize(hash = {})
@@ -26,10 +32,6 @@ class HashPath < Hash
26
32
  end
27
33
 
28
34
  private
29
- def rescue_hierarchical_access(key, e)
30
- return nil
31
- end
32
-
33
35
  def method_missing(name, *args, &block)
34
36
  if args.empty?
35
37
  self[ self.class.paths[name.to_s] || name ]
@@ -0,0 +1,6 @@
1
+ begin
2
+ require "jsonpath"
3
+ rescue
4
+ $stderr.puts "'jsonpath' gem is not found. Please install it first!\ngem install jsonpath"
5
+ raise
6
+ end
@@ -10,25 +10,8 @@ describe HashPath do
10
10
  HashPath.new.should respond_to(:[])
11
11
  end
12
12
 
13
- def data
14
- {
15
- "app" => {
16
- "models" => "ActiveRecord",
17
- "controllers" => "ActionController",
18
- "views" => "ActionView",
19
- "helpers" => "ActionView::Helpers",
20
- },
21
- "vendor" => {
22
- "plugins" => {
23
- "aasm" => "Aasm",
24
- "haml" => "Haml",
25
- },
26
- }
27
- }
28
- end
29
-
30
13
  describe "#[]" do
31
- subject { HashPath.new(data) }
14
+ subject { HashPath.new(data('rails.json')) }
32
15
 
33
16
  context "(for present)" do
34
17
  its("app/models") { should == "ActiveRecord" }
@@ -46,7 +29,7 @@ describe HashPath do
46
29
  path :nothing , "no/such/value"
47
30
  end
48
31
 
49
- subject { Labeled.new(data) }
32
+ subject { Labeled.new(data('rails.json')) }
50
33
 
51
34
  its(:model) { should == "ActiveRecord" }
52
35
  its("model") { should == "ActiveRecord" }
@@ -0,0 +1,8 @@
1
+ {
2
+ "name": "Buono",
3
+ "members": [
4
+ {"name": "momo", "age": 17},
5
+ {"name": "miya", "age": 17},
6
+ {"name": "airi", "age": 15}
7
+ ]
8
+ }
@@ -0,0 +1,14 @@
1
+ {
2
+ "app": {
3
+ "models": "ActiveRecord",
4
+ "controllers" : "ActionController",
5
+ "views" : "ActionView",
6
+ "helpers" : "ActionView::Helpers"
7
+ },
8
+ "vendor": {
9
+ "plugins": {
10
+ "haml" : "Haml",
11
+ "aasm" : "Aasm"
12
+ }
13
+ }
14
+ }
@@ -0,0 +1,29 @@
1
+
2
+ require File.join(File.dirname(__FILE__), 'spec_helper.rb')
3
+
4
+ describe HashPath do
5
+ class Group < HashPath
6
+ path :name , "name"
7
+ path :leader , "$..members[0]"
8
+ path :middle_school_member, "$..members[?(@['age'] <= 15)]"
9
+ end
10
+
11
+ it "should recognize jsonpath syntax when jsonpath is loaded" do
12
+ require 'hash-path/json'
13
+
14
+ buono = Group.new(data('group.json'))
15
+ momo = {"name"=> "momo", "age"=> 17}
16
+ airi = {"name"=> "airi", "age"=> 15}
17
+
18
+ buono.name.should == "Buono"
19
+ buono.leader.should == [momo]
20
+ buono.middle_school_member.should == [airi]
21
+
22
+ # remove const to disable 'jsonpath' gem
23
+ Object.instance_eval { remove_const :JsonPath }
24
+
25
+ buono.name.should == "Buono"
26
+ buono.leader.should == nil
27
+ buono.middle_school_member.should == nil
28
+ end
29
+ end
@@ -1,10 +1,19 @@
1
1
 
2
2
  require 'spec'
3
+ require 'pathname'
3
4
 
4
5
  require File.join(File.dirname(__FILE__), '/../lib/hash-path')
5
6
  require File.join(File.dirname(__FILE__), '/its_helper')
6
7
 
7
8
  def data(key)
8
- path = File.join(File.dirname(__FILE__) + "/fixtures/#{key}")
9
- File.read(path){}
9
+ path = Pathname(__FILE__).parent + "fixtures/#{key}"
10
+ buffer = File.read(path){}
11
+
12
+ case path.extname
13
+ when ".json"
14
+ require 'json'
15
+ buffer = JSON.parse(buffer)
16
+ end
17
+
18
+ return buffer
10
19
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hash-path
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - maiha
@@ -27,8 +27,12 @@ files:
27
27
  - README
28
28
  - Rakefile
29
29
  - lib/hash-path.rb
30
+ - lib/hash-path/json.rb
30
31
  - spec/inherit_spec.rb
31
32
  - spec/api_spec.rb
33
+ - spec/fixtures/rails.json
34
+ - spec/fixtures/group.json
35
+ - spec/jsonpath_spec.rb
32
36
  - spec/its_helper.rb
33
37
  - spec/spec_helper.rb
34
38
  has_rdoc: true