pathy 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/pathy.rb +25 -12
- data/lib/pathy/version.rb +1 -1
- data/spec/lib/pathy_spec.rb +34 -3
- data/spec/spec_helper.rb +4 -0
- metadata +3 -3
data/lib/pathy.rb
CHANGED
@@ -1,37 +1,50 @@
|
|
1
1
|
require 'json'
|
2
2
|
|
3
3
|
module Pathy
|
4
|
+
|
4
5
|
InvalidPathError = Class.new(NoMethodError)
|
5
6
|
|
6
7
|
module InstanceMethods
|
8
|
+
|
9
|
+
# returns the value parsed from JSON at a given path.
|
10
|
+
# Example:
|
11
|
+
# {:some_key => {:nested_key => 'awesome'}}.at_json_path('some_key.nested_key')
|
12
|
+
# returns 'awesome'
|
7
13
|
def at_json_path path
|
8
14
|
method_chain = path.split('.')
|
9
|
-
method_chain.inject(self.reparsed_as_json) do |obj,
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
15
|
+
method_chain.inject(self.reparsed_as_json) do |obj,method|
|
16
|
+
|
17
|
+
is_array_like = obj.respond_to?(:push)
|
18
|
+
key_or_index = is_array_like ? method.to_i : method
|
19
|
+
has_key_or_index = is_array_like ? !obj.slice(key_or_index).nil? : obj.keys.include?(key_or_index)
|
20
|
+
|
21
|
+
raise InvalidPathError, "Could not resolve #{path} at #{key_or_index}" unless has_key_or_index
|
22
|
+
|
23
|
+
obj.send('[]', key_or_index)
|
24
|
+
end
|
18
25
|
end
|
19
26
|
|
27
|
+
# returns true if the path is found. Provides usage in Rspec
|
28
|
+
# Example in rspec:
|
29
|
+
# {:some_key => {:nested_key => 'awesome'}}.should have_json_path('some_key.nested_key')
|
20
30
|
def has_json_path? path
|
21
31
|
begin
|
22
|
-
|
23
|
-
|
24
|
-
return true
|
32
|
+
at_json_path(path)
|
33
|
+
true
|
25
34
|
rescue InvalidPathError
|
26
35
|
false
|
27
36
|
end
|
28
37
|
end
|
29
38
|
|
39
|
+
# returns the parsed JSON representation of an instance.
|
40
|
+
# If the current instance is a string we assume it's JSON
|
30
41
|
def reparsed_as_json
|
31
42
|
self.is_a?(String) ? JSON.parse(self) : JSON.parse(self.to_json)
|
32
43
|
end
|
33
44
|
end
|
34
45
|
module ClassMethods
|
46
|
+
|
47
|
+
# adds pathy methods to a Class
|
35
48
|
def pathy!
|
36
49
|
self.send :include, InstanceMethods
|
37
50
|
end
|
data/lib/pathy/version.rb
CHANGED
data/spec/lib/pathy_spec.rb
CHANGED
@@ -1,9 +1,33 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Pathy do
|
4
|
-
before :all do
|
5
4
|
|
6
|
-
|
5
|
+
describe "pathy scope" do
|
6
|
+
|
7
|
+
describe "Object.new" do
|
8
|
+
it "should not respond to :has_json_path?" do
|
9
|
+
Object.new.should_not respond_to(:has_json_path?)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "SampleClass" do
|
14
|
+
before :all do
|
15
|
+
SampleClass.pathy!
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should not respond to :has_json_path?" do
|
19
|
+
Object.new.should_not respond_to(:has_json_path?)
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should respond to :has_json_path?" do
|
23
|
+
SampleClass.new.should respond_to(:has_json_path?)
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
before :all do
|
7
31
|
|
8
32
|
@json = %[
|
9
33
|
{
|
@@ -11,7 +35,8 @@ describe Pathy do
|
|
11
35
|
"number" : 1,
|
12
36
|
"array" : [1,2,3],
|
13
37
|
"hash" : {"one":{"two" : 2}},
|
14
|
-
"bool" : false
|
38
|
+
"bool" : false,
|
39
|
+
"nullval" : null
|
15
40
|
}
|
16
41
|
]
|
17
42
|
|
@@ -29,6 +54,8 @@ describe Pathy do
|
|
29
54
|
|
30
55
|
describe "for hashes" do
|
31
56
|
before :all do
|
57
|
+
Object.pathy!
|
58
|
+
|
32
59
|
@obj = JSON.parse(@json)
|
33
60
|
@array = JSON.parse(@json_array)
|
34
61
|
end
|
@@ -64,6 +91,10 @@ describe Pathy do
|
|
64
91
|
it "should have 'bool'" do
|
65
92
|
@obj.should have_json_path 'bool'
|
66
93
|
end
|
94
|
+
|
95
|
+
it "should have 'nullval'" do
|
96
|
+
@obj.should have_json_path 'nullval'
|
97
|
+
end
|
67
98
|
|
68
99
|
it "should be false for invalid paths" do
|
69
100
|
@obj.has_json_path?('hash.one.does_not_exist').should be_false
|
data/spec/spec_helper.rb
CHANGED
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:
|
4
|
+
hash: 23
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 4
|
10
|
+
version: 0.0.4
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Christopher Burnett
|