File: dig.rb

package info (click to toggle)
ruby-jsonpath 1.1.5-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 332 kB
  • sloc: ruby: 1,831; makefile: 4
file content (57 lines) | stat: -rw-r--r-- 1,414 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# frozen_string_literal: true

class JsonPath
  module Dig

    # Similar to what Hash#dig or Array#dig
    def dig(context, *keys)
      keys.inject(context){|memo,k|
        dig_one(memo, k)
      }
    end

    # Returns a hash mapping each key from keys
    # to its dig value on context.
    def dig_as_hash(context, keys)
      keys.each_with_object({}) do |k, memo|
        memo[k] = dig_one(context, k)
      end
    end

    # Dig the value of k on context.
    def dig_one(context, k)
      case context
      when Hash
        context[@options[:use_symbols] ? k.to_sym : k]
      when Array
        context[k.to_i]
      else
        if context.respond_to?(:dig)
          context.dig(k)
        elsif @options[:allow_send]
          context.__send__(k)
        end
      end
    end

    # Yields the block if context has a diggable
    # value for k
    def yield_if_diggable(context, k, &blk)
      case context
      when Array
        nil
      when Hash
        k = @options[:use_symbols] ? k.to_sym : k
        return yield if context.key?(k) || @options[:default_path_leaf_to_null]
      else
        if context.respond_to?(:dig)
          digged = dig_one(context, k)
          yield if !digged.nil? || @options[:default_path_leaf_to_null]
        elsif @options[:allow_send] && context.respond_to?(k.to_s) && !Object.respond_to?(k.to_s)
          yield
        end
      end
    end

  end
end