Module: Kind::Dig
Instance Method Summary collapse
- #[](*keys) ⇒ Object
- #call(data, *input) {|result| ... } ⇒ Object
-
#call!(data, keys = Empty::ARRAY) ⇒ Object
:nodoc.
- #presence(*args, &block) ⇒ Object
Instance Method Details
#[](*keys) ⇒ Object
35 36 37 |
# File 'lib/kind/dig.rb', line 35 def [](*keys) ->(data) { call!(data, keys) } end |
#call(data, *input) {|result| ... } ⇒ Object
21 22 23 24 25 26 27 28 29 |
# File 'lib/kind/dig.rb', line 21 def call(data, *input) args = input.size == 1 && input[0].kind_of?(::Array) ? input[0] : input result = call!(data, args) return result unless block_given? yield(result) unless KIND.nil_or_undefined?(result) end |
#call!(data, keys = Empty::ARRAY) ⇒ Object
:nodoc
11 12 13 14 15 16 17 18 19 |
# File 'lib/kind/dig.rb', line 11 def call!(data, keys = Empty::ARRAY) # :nodoc keys.reduce(data) do |memo, key| value = get(memo, key) break if KIND.nil_or_undefined?(value) value end end |
#presence(*args, &block) ⇒ Object
31 32 33 |
# File 'lib/kind/dig.rb', line 31 def presence(*args, &block) Presence.(call(*args, &block)) end |