Class: Stages::Wrap

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

Instance Attribute Summary

Attributes inherited from Stage

#invocations, #source

Instance Method Summary collapse

Methods inherited from Stage

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

Constructor Details

#initialize(pipeline, *args, &block) ⇒ Wrap

pass a block that takes two arguments that joins the original thing passed to the results, for each result



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/stages/wrap.rb', line 6

def initialize(pipeline, *args, &block)
  @feeder = Feeder.new
  @pipeline = @feeder | pipeline
  @output_style = :hash
  @block = block
  unless args.empty?
    if args.include? :array
      @output_style = :array
    elsif args.include? :each
      @output_style = :each
    end
    @aggregated = true if args.include? :aggregated
  end
  super()
end

Instance Method Details

#processObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/stages/wrap.rb', line 28

def process
  while !source_empty?
    value = input
    @feeder << value
    results = []
    while !@pipeline.done?
      v = @pipeline.run
      #yield each subpipeline result, as they are generated
      if @output_style == :each
        if @block
          output @block.call(value, v)
        else
          output v
        end
      else
        results << v
      end
    end
    if @output_style != :each
      results = results.first if @aggregated
      #note that block supercedes array or hash
      if @block
        output @block.call(value, results)
      else
        output results if @output_style == :array
        output({ value => results}) if @output_style == :hash
      end
    end
    @pipeline.reset
  end
  @pipeline.reset
end

#resetObject



22
23
24
25
26
# File 'lib/stages/wrap.rb', line 22

def reset
  initialize_loop
  @pipeline.reset
  @source.reset if @source
end