Class: MatchReduce::Aggregator
- Inherits:
-
Object
- Object
- MatchReduce::Aggregator
- 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
-
#group_keys ⇒ Object
readonly
Returns the value of attribute group_keys.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#patterns ⇒ Object
readonly
Returns the value of attribute patterns.
-
#reducer ⇒ Object
readonly
Returns the value of attribute reducer.
Instance Method Summary collapse
- #grouped? ⇒ Boolean
-
#initialize(name:, group_keys: [], patterns: [], reducer: nil) ⇒ Aggregator
constructor
A new instance of Aggregator.
- #keys ⇒ Object
- #reduce(memo, record, resolver) ⇒ Object
Constructor Details
#initialize(name:, group_keys: [], patterns: [], reducer: nil) ⇒ Aggregator
Returns a new instance of Aggregator.
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_keys ⇒ Object (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 |
#name ⇒ Object (readonly)
Returns the value of attribute name.
15 16 17 |
# File 'lib/match_reduce/aggregator.rb', line 15 def name @name end |
#patterns ⇒ Object (readonly)
Returns the value of attribute patterns.
15 16 17 |
# File 'lib/match_reduce/aggregator.rb', line 15 def patterns @patterns end |
#reducer ⇒ Object (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
39 40 41 |
# File 'lib/match_reduce/aggregator.rb', line 39 def grouped? !group_keys.empty? end |
#keys ⇒ Object
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 |