Sha256: 62adc7b59691e666d9f84cbcc33883ec9f476fba65151b68a8cc39e6afaff075

Contents?: true

Size: 1.15 KB

Versions: 9

Compression:

Stored size: 1.15 KB

Contents

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)
    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
      Marshal::load(Marshal::dump(@obj))
    end

    def _gsub(obj, path, replacement)
      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

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
jsonpath-0.5.8 lib/jsonpath/proxy.rb
jsonpath-0.5.7 lib/jsonpath/proxy.rb
jsonpath-0.5.6 lib/jsonpath/proxy.rb
jsonpath-0.5.5 lib/jsonpath/proxy.rb
jsonpath-0.5.4 lib/jsonpath/proxy.rb
jsonpath-0.5.3 lib/jsonpath/proxy.rb
jsonpath-0.5.2 lib/jsonpath/proxy.rb
jsonpath-0.5.1 lib/jsonpath/proxy.rb
jsonpath-0.5.0 lib/jsonpath/proxy.rb