module Hamster::Associable
def dig(key, *rest)
-
(Object)
-
def dig(key, *rest) value = get(key) if rest.empty? || value.nil? value elsif value.respond_to?(:dig) value.dig(*rest) end end
def update_in(*key_path, &block)
-
(Associable)
-
Other tags:
- Yieldreturn: - The new value to store
Other tags:
- Yield: - The previously stored value
Parameters:
-
key_path
(Object(s)
) -- List of keys/indexes which form the path to the key to be modified
def update_in(*key_path, &block) if key_path.empty? raise ArgumentError, "must have at least one key in path" end key = key_path[0] if key_path.size == 1 new_value = block.call(fetch(key, nil)) else value = fetch(key, EmptyHash) new_value = value.update_in(*key_path[1..-1], &block) end put(key, new_value) end