Skip to content

Commit

Permalink
testing
Browse files Browse the repository at this point in the history
  • 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.
63 changes: 63 additions & 0 deletions lib/wukong/widget/reducers/improver.rb
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

0 comments on commit 652b850

Please sign in to comment.