Class: PVC::Pipeline

Inherits:
Object
  • Object
show all
Defined in:
lib/pvc/pipeline.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ Pipeline

Returns a new instance of Pipeline.



15
16
17
18
19
20
# File 'lib/pvc/pipeline.rb', line 15

def initialize(*args, &block)
  @pieces = []
  if args.length > 0 || block_given?
    self.to(*args, &block)
  end
end

Instance Method Details

#input(input) ⇒ Object



33
34
35
36
# File 'lib/pvc/pipeline.rb', line 33

def input(input)
  @pieces << InputPiece.new(input)
  self
end

#only_errObject



43
44
45
46
# File 'lib/pvc/pipeline.rb', line 43

def only_err
  @pieces << OnlyErrPiece.new
  self
end

#runObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/pvc/pipeline.rb', line 48

def run
  runners = ([NullPiece.new] + @pieces + [ResultPiece.new]).map(&:runner)
  
  runners.zip(runners[1..-1]).reverse.each do |current, following|
    current.start(following)
  end

  runners.each do |current|
    current.finish
  end

  Result.new(
    :stdout => runners.last.stdout,
    :stderr => runners.last.stderr,
    :stdboth => runners.last.stdboth,
    :codes => runners.inject([]) { |codes, runner| codes << runner.code if runner.respond_to?(:code); codes },
    :returns => runners.inject([]) { |returns, runner| returns << runner.return if runner.respond_to?(:return); returns }
  )
end

#to(*args, &block) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/pvc/pipeline.rb', line 22

def to(*args, &block)
  if block_given?
    @pieces << BlockPiece.new(&block)
  elsif args.length == 1 && args.first.respond_to?(:pieces)
    args.first.pieces.each { |piece| @pieces << piece }
  else
    @pieces << ProcessPiece.new(*args)
  end
  self
end

#with_errObject



38
39
40
41
# File 'lib/pvc/pipeline.rb', line 38

def with_err
  @pieces << WithErrPiece.new
  self
end