Class: Attr::Gather::Aggregators::DeepMerge

Inherits:
Base
  • Object
show all
Defined in:
lib/attr/gather/aggregators/deep_merge.rb

Overview

Deep merges result hashes

Instance Method Summary collapse

Constructor Details

#initialize(reverse: false, merge_input: true, array_strategy: :concat) ⇒ DeepMerge

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initialize a new DeepMerge aggregator

Parameters:

  • reverse (Boolean) (defaults to: false)

    deep merge results in reverse order

  • merge_input (Boolean) (defaults to: true)

    merge the result with the initial input

  • array_strategy (Symbol) (defaults to: :concat)

    strategy for handling arrays, one of (:concat, :overwrite)



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/attr/gather/aggregators/deep_merge.rb', line 19

def initialize(reverse: false, merge_input: true, array_strategy: :concat, **)
  unless ARRAY_STRATEGY.include?(array_strategy)
    raise ArgumentError, 'array_strategy must be one of: :concat, :overwrite'
  end

  @reverse = reverse
  @merge_input = merge_input
  @array_strategy = array_strategy

  super
end

Instance Method Details

#call(input, execution_results) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/attr/gather/aggregators/deep_merge.rb', line 31

def call(input, execution_results)
  execution_results = execution_results.reverse_each if reverse?

  execution_results.reduce(@merge_input ? input : EMPTY_HASH) do |memo, res|
    deep_merge(memo, unwrap_result(res))
  end
end