Class: Wukong::Processor::Accumulator
- Inherits:
-
Wukong::Processor
- Object
- Hanuman::Stage
- Wukong::Processor
- Wukong::Processor::Accumulator
- Defined in:
- lib/wukong/widget/reducers/accumulator.rb
Overview
A base widget for building more complex accumulative widgets.
Constant Summary
Constants inherited from Wukong::Processor
Instance Attribute Summary collapse
-
#group ⇒ Object
The current group of records.
-
#key ⇒ Object
The current key used to define the current group being accumulated.
Attributes included from Hanuman::StageInstanceMethods
Instance Method Summary collapse
-
#accumulate(record) ⇒ Object
Accumulates another +record+.
-
#get_key(record) ⇒ Object
Gets the key from the given +record+.
-
#process(record) {|finalized_record| ... } ⇒ Object
Processes the
record
. -
#setup ⇒ Object
Sets up this accumulator by defining an initial key (with a value that is unlikely to be found in real data) and calling
#start
with no record. -
#start(record) ⇒ Object
Starts accumulation for a new group of records with a new key.
Methods inherited from Wukong::Processor
configure, description, #finalize, #perform_action, #receive_action, #stop
Methods included from Logging
Methods inherited from Hanuman::Stage
Methods included from Hanuman::StageClassMethods
#builder, #label, #register, #set_builder
Methods included from Hanuman::StageInstanceMethods
#add_stage_link, #linkable_name, #root
Instance Attribute Details
#group ⇒ Object
The current group of records.
12 13 14 |
# File 'lib/wukong/widget/reducers/accumulator.rb', line 12 def group @group end |
#key ⇒ Object
The current key used to define the current group being accumulated.
9 10 11 |
# File 'lib/wukong/widget/reducers/accumulator.rb', line 9 def key @key end |
Instance Method Details
#accumulate(record) ⇒ Object
Accumulates another +record+.
Does nothing by default, intended for you to override.
69 70 |
# File 'lib/wukong/widget/reducers/accumulator.rb', line 69 def accumulate record end |
#get_key(record) ⇒ Object
Gets the key from the given +record+. By default a record's key is just the record itself.
60 61 62 |
# File 'lib/wukong/widget/reducers/accumulator.rb', line 60 def get_key record record end |
#process(record) {|finalized_record| ... } ⇒ Object
Processes the record
.
If the record is part of the current group (has a key that is
the same as the current key) then will call accumulate
with
the record.
If the record has a different key, will call finalize
and
then call start
with the record.
38 39 40 41 42 43 44 45 46 |
# File 'lib/wukong/widget/reducers/accumulator.rb', line 38 def process(record) this_key = get_key(record) if this_key != self.key finalize { |record| yield record } unless self.key == :__first_group__ self.key = this_key start record end accumulate(record) end |
#setup ⇒ Object
Sets up this accumulator by defining an initial key (with a
value that is unlikely to be found in real data) and calling
#start
with no record.
17 18 19 20 |
# File 'lib/wukong/widget/reducers/accumulator.rb', line 17 def setup @key = :__first_group__ start(nil) end |
#start(record) ⇒ Object
Starts accumulation for a new group of records with a new key. This is where you can reset counters, clear caches, &c.
52 53 |
# File 'lib/wukong/widget/reducers/accumulator.rb', line 52 def start record end |