Class: Wukong::Processor::Accumulator

Inherits:
Wukong::Processor show all
Defined in:
lib/wukong/widget/reducers/accumulator.rb

Overview

A base widget for building more complex accumulative widgets.

Direct Known Subclasses

Bin, Count, Sort, Uniq

Constant Summary

Constants inherited from Wukong::Processor

SerializerError

Instance Attribute Summary collapse

Attributes included from Hanuman::StageInstanceMethods

#graph

Instance Method Summary collapse

Methods inherited from Wukong::Processor

configure, description, #finalize, #perform_action, #receive_action, #stop

Methods included from Logging

included

Methods inherited from Hanuman::Stage

#clone

Methods included from Hanuman::StageClassMethods

#builder, #label, #register, #set_builder

Methods included from Hanuman::StageInstanceMethods

#add_stage_link, #linkable_name, #root

Instance Attribute Details

#groupObject

The current group of records.



12
13
14
# File 'lib/wukong/widget/reducers/accumulator.rb', line 12

def group
  @group
end

#keyObject

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.

Parameters:

  • record (Object)


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.

Parameters:

  • record (Object)

Returns:

  • (Object)

    the record's key



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.

Parameters:

  • record (Object)

Yields:

  • (finalized_record)

    each record yielded by finalize

Yield Parameters:

  • finalized_record (Object)

See Also:



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

#setupObject

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.

Parameters:

  • record (Object)


52
53
# File 'lib/wukong/widget/reducers/accumulator.rb', line 52

def start record
end