Sha256: dd78a9e59989536ddf10f4d85056b8c6dc0ab5380b944a975adceb154c81f5da

Contents?: true

Size: 1.05 KB

Versions: 2

Compression:

Stored size: 1.05 KB

Contents

module Mandy
  module Reducers
    class Base
      KEY_VALUE_SEPERATOR = "\t" unless defined?(KEY_VALUE_SEPERATOR)
    
      def initialize(input=STDIN, output=STDOUT)
        @input, @output = input, output
      end
    
      def self.compile(&blk)
        Class.new(Mandy::Reducers::Base) do 
          self.class_eval do
            define_method(:reducer, blk) if blk
          end
        end
      end
    
      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(last_key, values)
      end
    
      def emit(key, value=nil)
        key = 'nil' if key.nil?
        @output.puts(value.nil? ? key.to_s : "#{key}\t#{value}")
      end
    
      private
    
      def reducer(key,values)
        #nil
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
trafficbroker-mandy-0.1.5 lib/reducers/base_reducer.rb
trafficbroker-mandy-0.1.6 lib/reducers/base_reducer.rb