Sha256: 9e3e9844a5944e828d8967ff66322c28369369fcbc314a1feab3ee943b8778f5

Contents?: true

Size: 492 Bytes

Versions: 3

Compression:

Stored size: 492 Bytes

Contents

require 'backport_dig' if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.3')

class Hash
  def deep_fetch(key, default = nil)
    keys = key.to_s.split('.')
    value = dig(*keys) rescue default
    value.nil? ? default : value  # value can be false (Boolean)
  end
end

class Array
  def deep_fetch(index, default = nil)
    indexes = index.to_s.split('.').map(&:to_i)
    value = dig(*indexes) rescue default
    value.nil? ? default : value  # value can be false (Boolean)
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
json_logic-0.4.7 lib/core_ext/deep_fetch.rb
json_logic-0.4.6 lib/core_ext/deep_fetch.rb
json_logic-0.4.5 lib/core_ext/deep_fetch.rb