Class: Wukong::Processor::Count

Inherits:
Accumulator show all
Defined in:
lib/wukong/widget/reducers/count.rb

Overview

A processor which counts the total number of its input records.

On it's own, this widget is really just a poor man's wc -l. It's really intended to serve as a superclass for more complex accumulators.

Examples:

Count the total number of input records on the command-line.


$ wc -l input
283 input
$ cat input | wu-local count
283

Direct Known Subclasses

Group

Constant Summary

Constants inherited from Wukong::Processor

SerializerError

Instance Attribute Summary collapse

Attributes inherited from Accumulator

#group, #key

Attributes included from Hanuman::StageInstanceMethods

#graph

Instance Method Summary collapse

Methods inherited from Accumulator

#process, #start

Methods inherited from Wukong::Processor

configure, description, #perform_action, #process, #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_link, #linkable_name, #root

Instance Attribute Details

#sizeObject

The total size of the input recors.



33
34
35
# File 'lib/wukong/widget/reducers/count.rb', line 33

def size
  @size
end

Instance Method Details

#accumulate(record) ⇒ Object

Accumulate a record by incrmenting the total size.

Parameters:

  • record (Object)


44
45
46
# File 'lib/wukong/widget/reducers/count.rb', line 44

def accumulate record
  self.size += 1
end

#finalize {|size| ... } ⇒ Object

Yields the total size.

Yields:

Yield Parameters:

  • size (Integer)


66
67
68
# File 'lib/wukong/widget/reducers/count.rb', line 66

def finalize
  yield self.size
end

#get_key(record) ⇒ :__first__group__

Keeps all records in the same group so that one count is emitted at the end.

Overriding this method and returning different keys for different records is the beginning of constructing a "group by" type widget.

Parameters:

  • record (Object)

Returns:

  • (:__first__group__)

See Also:



58
59
60
# File 'lib/wukong/widget/reducers/count.rb', line 58

def get_key record
  :__first_group__
end

#setupObject

Initializes the count to 0.



36
37
38
39
# File 'lib/wukong/widget/reducers/count.rb', line 36

def setup
  super()
  @size = 0
end