Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

updating README

  • Loading branch information...
commit 7e8da9a8d628298ece24fbd0ccccdecdebf6b33e 1 parent 79359e9
@twoism authored
View
132 README.md
@@ -6,120 +6,44 @@
gem install pathy
-### Usage ###
-
- require 'spec_helper'
-
- describe Pathy do
- before :all do
-
- # add the helper methods to any object.
- # this can be called on any class.
- Object.pathy!
-
- @json = %[
- {
- "string" : "barr",
- "number" : 1,
- "array" : [1,2,3],
- "hash" : {"one":{"two" : 2}}
- }
- ]
-
- @json_array = %[
- [{
- "string" : "barr",
- "number" : 1,
- "array" : [1,2,3],
- "hash" : {"one":{"two" : 2}}
- }]
- ]
-
- end
-
-
- describe "for hashes" do
- before :all do
- @obj = JSON.parse(@json)
- @array = JSON.parse(@json_array)
- end
-
- it "should parse 'number' as 1" do
- @obj.at_json_path("number").should == 1
- end
-
- it "should parse 'array' as [1,2,3]" do
- @obj.at_json_path('array').should == [1,2,3]
- end
-
- it "should parse 'hash.one' as {'two' => 2}" do
- @json.at_json_path('hash.one').should == {'two' => 2}
- end
+ In Rails
- it "should parse 'hash.one' as {'two': 2}" do
- @obj.at_json_path('hash.one.two').should == 2
- end
+ gem 'pathy'
- describe "invalid paths" do
- it "should raise InvalidPathError" do
- lambda {
- @obj.at_json_path('foo.bar')
- }.should raise_error Pathy::InvalidPathError
- end
- end
-
- describe "#has_json_path?" do
- it "should be true for valid paths" do
- @obj.has_json_path?('hash.one.two').should be_true
- end
- it "should be false for invalid paths" do
- @obj.has_json_path?('hash.one.foo').should be_false
- end
-
- it "should work as rspec matcher" do
- @obj.should have_json_path "hash.one"
- end
-
- end
-
-
- end
-
- describe "for arrays" do
-
- before :all do
- @array = JSON.parse(@json_array)
- end
-
- it "should find the index" do
- @array.at_json_path('0.hash.one.two').should == 2
- end
+### Usage ###
- end
+ Activate pathy for all objects
- describe "for json strings" do
+ Object.pathy!
- it "should parse 'number' as 1" do
- @json.at_json_path("number").should == 1
- end
+ This adds the conveinece methods to any object
- it "should parse 'array' as [1,2,3]" do
- @json.at_json_path('array').should == [1,2,3]
- end
+```ruby
+@obj = %[
+ {
+ "string" : "barr",
+ "number" : 1,
+ "array" : [1,2,3],
+ "hash" : {"one":{"two" : 2}}
+ }
+]
- it "should parse 'hash.one' as {'two' => 2}" do
- @json.at_json_path('hash.one').should == {'two' => 2}
- end
-
- it "should parse 'hash.one.two' as 2" do
- @json.at_json_path('hash.one.two').should == 2
- end
+puts @obj.at_json_path("number")
+=> 1
+puts @obj.at_json_path('array')
+=> [1,2,3]
+@json.at_json_path('hash.one')
+=> {'two' => 2}
+```
- end
+###RSpec Matcher###
- end
+```ruby
+it "should work as rspec matcher" do
+ @obj.should have_json_path "hash.one"
+end
+```
-
##Note on Patches/Pull Requests
View
0  autotest/discover.rb → autotest/discovery.rb
File renamed without changes
View
13 lib/pathy.rb
@@ -11,16 +11,15 @@ module InstanceMethods
# {:some_key => {:nested_key => 'awesome'}}.at_json_path('some_key.nested_key')
# returns 'awesome'
def at_json_path path
- method_chain = path.split('.')
- method_chain.inject(self.reparsed_as_json) do |obj,method|
+ path.split('.').inject( reparsed_from_json ) do |reparsed_self, method|
- is_array_like = obj.respond_to?(:push)
+ is_array_like = reparsed_self.respond_to?(:push)
key_or_index = is_array_like ? method.to_i : method
- has_key_or_index = is_array_like ? !obj.slice(key_or_index).nil? : obj.keys.include?(key_or_index)
+ has_key_or_index = is_array_like ? !reparsed_self.slice(key_or_index).nil? : reparsed_self.keys.include?(key_or_index)
raise InvalidPathError, "Could not resolve #{path} at #{key_or_index}" unless has_key_or_index
- obj.send('[]', key_or_index)
+ reparsed_self.send '[]', key_or_index
end
end
@@ -29,7 +28,7 @@ def at_json_path path
# {:some_key => {:nested_key => 'awesome'}}.should have_json_path('some_key.nested_key')
def has_json_path? path
begin
- at_json_path(path)
+ at_json_path path
true
rescue InvalidPathError
false
@@ -38,7 +37,7 @@ def has_json_path? path
# returns the parsed JSON representation of an instance.
# If the current instance is a string we assume it's JSON
- def reparsed_as_json
+ def reparsed_from_json
self.is_a?(String) ? JSON.parse(self) : JSON.parse(self.to_json)
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.