Class: Mandy::Reducers::Base

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

Constant Summary

Constants inherited from Task

Task::JSON_PAYLOAD_KEY, 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(&blk) ⇒ Object



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

def self.compile(&blk)
  Class.new(Mandy::Reducers::Base) do 
    self.class_eval do
      define_method(:reducer, blk) if blk
    end
  end
end

Instance Method Details

#executeObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/reducers/base_reducer.rb', line 14

def execute
  last_key, values = nil, []
  @input.each_line do |line|
     key, value = line.split(KEY_VALUE_SEPERATOR)
     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) })
end