Module: Revector::Utility
- Defined in:
- lib/revector/utility.rb
Defined Under Namespace
Modules: Keys
Constant Summary collapse
- TryFetchOrBlank =
hash = { a: 1, b: { c: 2 }, d: [{ e: 3 }] } Utility::TryFetchOrBlank.(hash, ‘d..e’) ““
->(data, *arr_keys) do reducer = ->(memo, key) do symbol_keys = *Keys.to_dig(key) deep_value = symbol_keys.reduce(memo) do |acc, attr| acc = acc.compact case acc in [::Integer, _] | [::String, _] | Integer acc[attr] in ::Array case acc in [::Hash, _] case attr in ::Integer acc&.dig(attr) else acc&.flat_map { |x| x&.dig(attr) } end else case attr in ::Integer | ::String acc&.dig(attr) else acc&.flat_map { |x| x.respond_to?(:dig) ? x&.dig(attr) : x } end end in ::Hash acc&.dig(attr) else acc end end deep_value rescue StandardError => e nil end arr_keys.reduce(data, &reducer) end