Class: Ralph

Inherits:
Object
  • Object
show all
Defined in:
lib/ralph.rb,
lib/iterator.rb

Defined Under Namespace

Modules: Iterator Classes: Job

Constant Summary collapse

VERSION =
'0.1.0'
@@map_input =
nil

Class Method Summary collapse

Class Method Details

.job(&block) ⇒ Object



55
56
57
# File 'lib/ralph.rb', line 55

def self.job(&block)
  self.instance_eval(&block) if block_given?# setup
end

.mapper(format = nil, &block) ⇒ Object



20
21
22
23
# File 'lib/ralph.rb', line 20

def self.mapper(format = nil, &block)
  Job.send(:define_method, :map, &block)
  @@map_input = format
end

.reducer(&block) ⇒ Object



25
26
27
# File 'lib/ralph.rb', line 25

def self.reducer(&block)
  Job.send(:define_method, :reduce, &block)
end

.run_mapper!(input = STDIN, output = STDOUT) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ralph.rb', line 29

def self.run_mapper!(input = STDIN, output = STDOUT)
  j = Job.new
  j.output = output
  if @@map_input == :csv
    CSVScan.scan(input) do |row|
      j.map(row)
    end
  else
    input.each_line do |line| 
      j.map(line)
    end
  end
end

.run_reducer!(input = STDIN, output = STDOUT) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ralph.rb', line 43

def self.run_reducer!(input = STDIN, output = STDOUT)
  j = Job.new
  j.output = output
  Iterator.input = input
  Iterator.grab! unless input.eof?
  until input.eof?
    Iterator.ready!
    j.reduce(Iterator.current_key, Iterator)
    Iterator.next_group! unless Iterator.start_of_group?
  end
end