Class: Attr::Gather::Aggregators::Base Abstract

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

Overview

This class is abstract.

Subclass and override #call to implement a custom Aggregator class.

Direct Known Subclasses

DeepMerge, ShallowMerge

Constant Summary collapse

NOOP_FILTER =
Filters::Noop.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**opts) ⇒ Base

Returns a new instance of Base.



18
19
20
# File 'lib/attr/gather/aggregators/base.rb', line 18

def initialize(**opts)
  @filter = opts.delete(:filter) || NOOP_FILTER
end

Instance Attribute Details

#filterAttr::Gather::Filters::Base

Returns filter for the output data.

Returns:



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/attr/gather/aggregators/base.rb', line 13

class Base
  attr_accessor :filter

  NOOP_FILTER = Filters::Noop.new

  def initialize(**opts)
    @filter = opts.delete(:filter) || NOOP_FILTER
  end

  def with(**opts)
    self.class.new(filter: @filter, **opts)
  end

  def call(_original_input, _results_array)
    raise NotImplementedError
  end

  private

  def unwrap_result(res)
    return res if filter.nil?

    filter.call(res).value
  end
end

Instance Method Details

#call(_original_input, _results_array) ⇒ Object

Raises:

  • (NotImplementedError)


26
27
28
# File 'lib/attr/gather/aggregators/base.rb', line 26

def call(_original_input, _results_array)
  raise NotImplementedError
end

#with(**opts) ⇒ Object



22
23
24
# File 'lib/attr/gather/aggregators/base.rb', line 22

def with(**opts)
  self.class.new(filter: @filter, **opts)
end