Class: MatchReduce::Aggregator

Inherits:
Object
  • Object
show all
Defined in:
lib/match_reduce/aggregator.rb

Overview

An aggregator is a group of patterns with a reducer that you wish to report on.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, group_keys: [], patterns: [], reducer: nil) ⇒ Aggregator

Returns a new instance of Aggregator.

Raises:

  • (ArgumentError)


20
21
22
23
24
25
26
27
28
29
# File 'lib/match_reduce/aggregator.rb', line 20

def initialize(name:, group_keys: [], patterns: [], reducer: nil)
  raise ArgumentError, 'name is required' if name.to_s.empty?

  @name       = name
  @group_keys = Array(group_keys)
  @patterns   = stringed_keys(ensure_not_empty(array(patterns)))
  @reducer    = reducer

  freeze
end

Instance Attribute Details

#group_keysObject (readonly)

Returns the value of attribute group_keys.



15
16
17
# File 'lib/match_reduce/aggregator.rb', line 15

def group_keys
  @group_keys
end

#nameObject (readonly)

Returns the value of attribute name.



15
16
17
# File 'lib/match_reduce/aggregator.rb', line 15

def name
  @name
end

#patternsObject (readonly)

Returns the value of attribute patterns.



15
16
17
# File 'lib/match_reduce/aggregator.rb', line 15

def patterns
  @patterns
end

#reducerObject (readonly)

Returns the value of attribute reducer.



15
16
17
# File 'lib/match_reduce/aggregator.rb', line 15

def reducer
  @reducer
end

Instance Method Details

#grouped?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/match_reduce/aggregator.rb', line 39

def grouped?
  !group_keys.empty?
end

#keysObject



31
32
33
# File 'lib/match_reduce/aggregator.rb', line 31

def keys
  patterns.flat_map(&:keys)
end

#reduce(memo, record, resolver) ⇒ Object



35
36
37
# File 'lib/match_reduce/aggregator.rb', line 35

def reduce(memo, record, resolver)
  reducer ? reducer.call(memo, record, resolver) : memo
end