Class: Stages::Each

Inherits:
Stage
  • Object
show all
Defined in:
lib/stages/each.rb

Instance Attribute Summary

Attributes inherited from Stage

#source

Instance Method Summary collapse

Methods inherited from Stage

#die, #done?, #drop_leftmost!, #each, #end?, #handle_value, #initialize_loop, #input, #length, #output, #reset, #root_source, #run, #source_empty?, #to_enum, #|

Constructor Details

#initialize(things = nil, &block) ⇒ Each

Returns a new instance of Each.



3
4
5
6
7
# File 'lib/stages/each.rb', line 3

def initialize(things = nil, &block)
  @things = things unless things.nil?
  @block = block
  super()
end

Instance Method Details

#processObject



9
10
11
12
13
14
15
# File 'lib/stages/each.rb', line 9

def process
  if @things
    process_things
  else
    process_inputs
  end
end

#process_inputsObject



17
18
19
20
21
22
23
24
25
# File 'lib/stages/each.rb', line 17

def process_inputs
  while !source_empty?
    v = input
    v = @block.call(v) if @block
    v.each do |v|
      handle_value v
    end
  end
end

#process_thingsObject



27
28
29
30
31
32
# File 'lib/stages/each.rb', line 27

def process_things
  @things = @block.call(@things) if @block
  @things.each do |thing|
    handle_value thing
  end
end