diff --git a/lib/jsonpath.rb b/lib/jsonpath.rb index 2465800..a9d676e 100644 --- a/lib/jsonpath.rb +++ b/lib/jsonpath.rb @@ -8,7 +8,7 @@ class JsonPath PATH_ALL = '$..*' - attr_reader :path + attr_accessor :path def initialize(path, opts = nil) @opts = opts @@ -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 @@ -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 diff --git a/test/test_jsonpath.rb b/test/test_jsonpath.rb index 40667b7..d0eb839 100644 --- a/test/test_jsonpath.rb +++ b/test/test_jsonpath.rb @@ -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