-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jacob Perkins
authored and
Erik Mackdanz
committed
Mar 5, 2014
1 parent
0e547de
commit 652b850
Showing
1 changed file
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
module Wukong | ||
class Processor | ||
|
||
# A base widget for building more complex improver widgets. | ||
class Improver < Processor | ||
|
||
# The current group of records. | ||
attr_accessor :group | ||
|
||
# Sets up this improver by defining an initial key (with a | ||
# value that is unlikely to be found in real data) and calling | ||
# `#zero` with no record. | ||
def setup | ||
@key = :__first_group__ | ||
zero | ||
end | ||
|
||
def recordize record | ||
record.split("\t") | ||
end | ||
|
||
# | ||
# All kinds of assumptions here, | ||
# record is tab-delimited and the | ||
# first field is a name of a function | ||
# to call | ||
# | ||
def get_function record | ||
record.first | ||
end | ||
|
||
# Processes the `record`. | ||
def process(record) | ||
fields = recordize(record) | ||
func = get_function(fields) | ||
case func | ||
when 'zero' then | ||
yield zero | ||
when 'accumulate' then | ||
accumulate(fields[1..-1]) | ||
when 'improve' then | ||
yield improve(fields.first, self.group) | ||
self.group = [] | ||
else | ||
raise NoMethodError, "undefined method #{func} for Improver" | ||
end | ||
end | ||
|
||
# Starts accumulation for a new key. Return what you would | ||
# with no improvements. | ||
def zero | ||
self.group = [] | ||
end | ||
|
||
# Accumulates another +record+. | ||
# | ||
# @param [Object] record | ||
def accumulate record | ||
self.group << record | ||
end | ||
end | ||
end | ||
end |