Class: Mandy::Reducers::Base

Inherits:
Task
  • Object
show all
Includes:
IO::OutputFormatting
Defined in:
lib/mandy/reducers/base_reducer.rb

Constant Summary

Constants inherited from Task

Task::DEFAULT_COUNTER_GROUP, Task::KEY_VALUE_SEPERATOR, Task::NUMERIC_PADDING

Instance Attribute Summary

Attributes inherited from Task

#input_format, #output_format

Class Method Summary collapse

Instance Method Summary collapse

Methods included from IO::OutputFormatting

#output_serialize_key, #output_serialize_value

Methods inherited from Task

#emit, #get, #initialize, #put

Constructor Details

This class inherits a constructor from Mandy::Task

Class Method Details

.compile(opts = {}, &blk) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/mandy/reducers/base_reducer.rb', line 6

def self.compile(opts={}, &blk)
  Class.new(Mandy::Reducers::Base) do 
    self.class_eval do
      define_method(:reducer, blk) if blk
      define_method(:setup, opts[:setup]) if opts[:setup]
      define_method(:teardown, opts[:teardown]) if opts[:teardown]
    end
  end
end

Instance Method Details

#executeObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mandy/reducers/base_reducer.rb', line 16

def execute
  setup if self.respond_to?(:setup)
  last_key, values = nil, []
  @input.each_line do |line|
     key, value = line.split(KEY_VALUE_SEPERATOR, 2)
     value.chomp!
     last_key = key if last_key.nil?
     if key != last_key
       reducer(last_key, values)
       last_key, values = key, []
     end
     values << value
  end
  reducer(deserialize_key(last_key), values.map {|v| deserialize_value(v) })
  teardown if self.respond_to?(:teardown)
end