pathy 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/lib/pathy.rb CHANGED
@@ -7,15 +7,24 @@ module Pathy
7
7
  def at_json_path path
8
8
  method_chain = path.split('.')
9
9
  method_chain.inject(self.reparsed_as_json) do |obj,m|
10
- key = (obj.respond_to?(:push) ? m.to_i : m)
11
- obj.send('[]', key) rescue raise InvalidPathError, "Could not resolve #{path} at #{key}"
12
- end
10
+ key = (obj.respond_to?(:push) ? m.to_i : m)
11
+ new_object = obj.send('[]', key)
12
+
13
+ raise InvalidPathError, "Could not resolve #{path} at #{key}" if obj[key].nil?
14
+ new_object
15
+ end
16
+ rescue
17
+ raise InvalidPathError, "Could not resolve #{path}"
13
18
  end
14
19
 
15
20
  def has_json_path? path
16
- !!self.at_json_path(path)
17
- rescue InvalidPathError
18
- false
21
+ begin
22
+ value = self.at_json_path(path)
23
+ return true if value == false
24
+ return true
25
+ rescue InvalidPathError
26
+ false
27
+ end
19
28
  end
20
29
 
21
30
  def reparsed_as_json
data/lib/pathy/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Pathy
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -10,7 +10,8 @@ describe Pathy do
10
10
  "string" : "barr",
11
11
  "number" : 1,
12
12
  "array" : [1,2,3],
13
- "hash" : {"one":{"two" : 2}}
13
+ "hash" : {"one":{"two" : 2}},
14
+ "bool" : false
14
15
  }
15
16
  ]
16
17
 
@@ -43,7 +44,6 @@ describe Pathy do
43
44
  it "should parse 'hash.one' as {'two' => 2}" do
44
45
  @json.at_json_path('hash.one').should == {'two' => 2}
45
46
  end
46
-
47
47
  it "should parse 'hash.one' as {'two': 2}" do
48
48
  @obj.at_json_path('hash.one.two').should == 2
49
49
  end
@@ -60,8 +60,13 @@ describe Pathy do
60
60
  it "should be true for valid paths" do
61
61
  @obj.has_json_path?('hash.one.two').should be_true
62
62
  end
63
+
64
+ it "should have 'bool'" do
65
+ @obj.should have_json_path 'bool'
66
+ end
67
+
63
68
  it "should be false for invalid paths" do
64
- @obj.has_json_path?('hash.one.foo').should be_false
69
+ @obj.has_json_path?('hash.one.does_not_exist').should be_false
65
70
  end
66
71
 
67
72
  it "should work as rspec matcher" do
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pathy
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Christopher Burnett