lib/jsonpath/proxy.rb in jsonpath-0.4.2 vs lib/jsonpath/proxy.rb in jsonpath-0.5.0
- old
+ new
@@ -1,25 +1,54 @@
class JsonPath
class Proxy
+ attr_reader :obj
+ alias_method :to_hash, :obj
+
def initialize(obj)
@obj = obj
end
def gsub(path, replacement = nil, &replacement_block)
- _gsub(deep_copy, path, replacement ? proc{replacement} : replacement_block)
+ _gsub(_deep_copy, path, replacement ? proc{replacement} : replacement_block)
end
def gsub!(path, replacement = nil, &replacement_block)
_gsub(@obj, path, replacement ? proc{replacement} : replacement_block)
end
+ def delete(path = JsonPath::PATH_ALL)
+ _delete(_deep_copy, path)
+ end
+
+ def delete!(path = JsonPath::PATH_ALL)
+ _delete(@obj, path)
+ end
+
+ def compact(path = JsonPath::PATH_ALL)
+ _compact(_deep_copy, path)
+ end
+
+ def compact!(path = JsonPath::PATH_ALL)
+ _compact(@obj, path)
+ end
+
private
- def deep_copy
+ def _deep_copy
Marshal::load(Marshal::dump(@obj))
end
def _gsub(obj, path, replacement)
- JsonPath.new(path, :mode => :substitute).enum_on(obj).each(&replacement)
- obj
+ JsonPath.new(path)[obj, :substitute].each(&replacement)
+ Proxy.new(obj)
+ end
+
+ def _delete(obj, path)
+ JsonPath.new(path)[obj, :delete].each
+ Proxy.new(obj)
+ end
+
+ def _compact(obj, path)
+ JsonPath.new(path)[obj, :compact].each
+ Proxy.new(obj)
end
end
end
\ No newline at end of file