Module: Broi::Input::Utils

Extended by:
Utils
Included in:
Utils
Defined in:
lib/broi/input/utils.rb

Instance Method Summary collapse

Instance Method Details

#deep_merge(target, source, &block) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/broi/input/utils.rb', line 14

def deep_merge(target, source, &block)
  if target.is_a?(Hash) && source.is_a?(Hash)
    keys = (target.keys + source.keys).uniq
    keys.map do |key|
      value = if [target, source].all? { |h| h.key? key }
        deep_merge(target[key], source[key], &block)
      else
        [target[key], source[key]].compact.first
      end
      [key, value]
    end.to_h
  else
    block ||= ->(_target, source) { source }
    block.(target, source)
  end
end

#deep_transform_values(object, &block) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/broi/input/utils.rb', line 6

def deep_transform_values(object, &block)
  if object.is_a?(Hash)
    object.transform_values { |value| deep_transform_values(value, &block) }
  else
    block.(object)
  end
end