Module: JsonPath::Dig
- Included in:
- Enumerable, Parser
- Defined in:
- lib/jsonpath/dig.rb
Instance Method Summary collapse
-
#dig(context, *keys) ⇒ Object
Similar to what Hash#dig or Array#dig.
-
#dig_as_hash(context, keys) ⇒ Object
Returns a hash mapping each key from keys to its dig value on context.
-
#dig_one(context, k) ⇒ Object
Dig the value of k on context.
-
#yield_if_diggable(context, k, &blk) ⇒ Object
Yields the block if context has a diggable value for k.
Instance Method Details
#dig(context, *keys) ⇒ Object
Similar to what Hash#dig or Array#dig
7 8 9 10 11 |
# File 'lib/jsonpath/dig.rb', line 7 def dig(context, *keys) keys.inject(context){|memo,k| dig_one(memo, k) } end |
#dig_as_hash(context, keys) ⇒ Object
Returns a hash mapping each key from keys to its dig value on context.
15 16 17 18 19 |
# File 'lib/jsonpath/dig.rb', line 15 def dig_as_hash(context, keys) keys.each_with_object({}) do |k, memo| memo[k] = dig_one(context, k) end end |
#dig_one(context, k) ⇒ Object
Dig the value of k on context.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/jsonpath/dig.rb', line 22 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 |
#yield_if_diggable(context, k, &blk) ⇒ Object
Yields the block if context has a diggable value for k
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/jsonpath/dig.rb', line 39 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 |