Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion lib/jsonpath.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class JsonPath

PATH_ALL = '$..*'

attr_reader :path
attr_accessor :path

def initialize(path, opts = nil)
@opts = opts
Expand Down Expand Up @@ -52,6 +52,12 @@ def initialize(path, opts = nil)
end
end

def join(join_path)
res = deep_clone
res.path += JsonPath.new(join_path).path
res
end

def on(obj_or_str)
enum_on(obj_or_str).to_a
end
Expand All @@ -77,4 +83,8 @@ def self.for(obj_or_str)
def self.process_object(obj_or_str)
obj_or_str.is_a?(String) ? MultiJson.decode(obj_or_str) : obj_or_str
end

def deep_clone
Marshal.load Marshal.dump(self)
end
end
4 changes: 4 additions & 0 deletions test/test_jsonpath.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ def test_class_method
assert_equal JsonPath.new('$..author').on(@object), JsonPath.on(@object, '$..author')
end

def test_join
assert_equal JsonPath.new('$.store.book..author').on(@object), JsonPath.new('$.store').join('book..author').on(@object)
end

def test_gsub
@object2['store']['bicycle']['price'] += 10
@object2['store']['book'][0]['price'] += 10
Expand Down