Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

bug fix for intended false values

  • Loading branch information...
commit 17c73d024eccf36728eb6e58d2e6da4f32355b44 1 parent a122f7d
@twoism authored
Showing with 24 additions and 10 deletions.
  1. +15 −6 lib/pathy.rb
  2. +1 −1  lib/pathy/version.rb
  3. +8 −3 spec/lib/pathy_spec.rb
View
21 lib/pathy.rb
@@ -7,15 +7,24 @@ module InstanceMethods
def at_json_path path
method_chain = path.split('.')
method_chain.inject(self.reparsed_as_json) do |obj,m|
- key = (obj.respond_to?(:push) ? m.to_i : m)
- obj.send('[]', key) rescue raise InvalidPathError, "Could not resolve #{path} at #{key}"
- end
+ key = (obj.respond_to?(:push) ? m.to_i : m)
+ new_object = obj.send('[]', key)
+
+ raise InvalidPathError, "Could not resolve #{path} at #{key}" if obj[key].nil?
+ new_object
+ end
+ rescue
+ raise InvalidPathError, "Could not resolve #{path}"
end
def has_json_path? path
- !!self.at_json_path(path)
- rescue InvalidPathError
- false
+ begin
+ value = self.at_json_path(path)
+ return true if value == false
+ return true
+ rescue InvalidPathError
+ false
+ end
end
def reparsed_as_json
View
2  lib/pathy/version.rb
@@ -1,3 +1,3 @@
module Pathy
- VERSION = "0.0.2"
+ VERSION = "0.0.3"
end
View
11 spec/lib/pathy_spec.rb
@@ -10,7 +10,8 @@
"string" : "barr",
"number" : 1,
"array" : [1,2,3],
- "hash" : {"one":{"two" : 2}}
+ "hash" : {"one":{"two" : 2}},
+ "bool" : false
}
]
@@ -43,7 +44,6 @@
it "should parse 'hash.one' as {'two' => 2}" do
@json.at_json_path('hash.one').should == {'two' => 2}
end
-
it "should parse 'hash.one' as {'two': 2}" do
@obj.at_json_path('hash.one.two').should == 2
end
@@ -60,8 +60,13 @@
it "should be true for valid paths" do
@obj.has_json_path?('hash.one.two').should be_true
end
+
+ it "should have 'bool'" do
+ @obj.should have_json_path 'bool'
+ end
+
it "should be false for invalid paths" do
- @obj.has_json_path?('hash.one.foo').should be_false
+ @obj.has_json_path?('hash.one.does_not_exist').should be_false
end
it "should work as rspec matcher" do
Please sign in to comment.
Something went wrong with that request. Please try again.