Sha256: 2c1866b953f162b17b44d3f3640e647810f6527ec1704758f7d7d432ce166588

Contents?: true

Size: 953 Bytes

Versions: 1

Compression:

Stored size: 953 Bytes

Contents

# frozen_string_literal: true

require 'dotpath/version'

module Dotpath
  DELIMITER = '.'

  autoload :Walk, 'dotpath/walk'
  autoload :Mutate, 'dotpath/mutate'
  autoload :Extension, 'dotpath/extension'

  def self.encode(key)
    key = key.to_s
    key.include?(DELIMITER) ? "\"#{key}\"" : key
  end

  # Retrieve a value at a given JSON path for an Array or Hash.
  #
  # @param object [Hash, Array] The object that can be navigated using JSON path.
  # @param json_path [String] The JSON path for the requested value.
  #
  # @return the value at JSON path.
  def self.value_at_path(object, json_path)
    raise ArgumentError, "#{object.class} does not support JSON path" unless object.respond_to?(:each_with_json_path)

    val = nil
    object.each_with_json_path do |path, value|
      if json_path == path
        val = value
        break
      end
    end
    val
  end
end

Hash.prepend(Dotpath::Extension)
Array.prepend(Dotpath::Extension)

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dotpath-0.1.0 lib/dotpath.rb