-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Factorize
dig
logic ; add a few options ; more tests ; fix README (#…
…137) * Add a few tests to improve coverage and highlight pending behaviors. * Factorize digging logic to a Dig helper module. * Add support for a :use_symbols option. * Add support for objects responding to dig. * Fix digging behavior in presence of explicit null/nil. * Fix & test README claims, document available options. Also includes a library fix when default_path_leaf_to_null is used. * Introduce :allow_send and document it (defaults to true).
- Loading branch information
Showing
7 changed files
with
449 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.