Class: Coque::Pipeline

Inherits:
Object
  • Object
show all
Includes:
Redirectable, Runnable
Defined in:
lib/coque/pipeline.rb

Instance Attribute Summary collapse

Attributes included from Redirectable

#stderr, #stdin, #stdout

Instance Method Summary collapse

Methods included from Runnable

#log_start, #run, #run!, #success?, #to_a, #to_a!

Methods included from Redirectable

#<, #>, #>=, #err, #getio, #in, #out

Constructor Details

#initialize(commands = []) ⇒ Pipeline

Returns a new instance of Pipeline.



7
8
9
# File 'lib/coque/pipeline.rb', line 7

def initialize(commands = [])
  @commands = commands
end

Instance Attribute Details

#commandsObject (readonly)

Returns the value of attribute commands.



6
7
8
# File 'lib/coque/pipeline.rb', line 6

def commands
  @commands
end

Instance Method Details

#cloneObject



11
12
13
# File 'lib/coque/pipeline.rb', line 11

def clone
  self.class.new(commands)
end

#get_resultObject



60
61
62
63
64
# File 'lib/coque/pipeline.rb', line 60

def get_result
  stdout = stitch
  results = commands.map(&:run)
  Result.new(results.last.pid, stdout)
end

#pipe(other) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/coque/pipeline.rb', line 19

def pipe(other)
  case other
  when Pipeline
    Pipeline.new(commands + other.commands)
  when Cmd
    Pipeline.new(commands + [other.clone])
  end
end

#stitchObject



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
# File 'lib/coque/pipeline.rb', line 32

def stitch
  # Set head in
  if commands.first.stdin.nil?
    start_r, start_w = IO.pipe
    start_w.close
    commands.first.stdin = start_r
  end

  # Connect intermediate in/outs
  commands.each_cons(2) do |left, right|
    read, write = IO.pipe
    left.stdout = write
    right.stdin = read
  end

  # Set tail out
  if self.stdout
    commands.last.stdout = stdout
    stdout
  elsif commands.last.stdout
    commands.last.stdout
  else
    next_r, next_w = IO.pipe
    commands.last.stdout = next_w
    next_r
  end
end

#to_sObject



15
16
17
# File 'lib/coque/pipeline.rb', line 15

def to_s
  "<Pipeline #{commands.join(" | ")} >"
end

#|(other) ⇒ Object



28
29
30
# File 'lib/coque/pipeline.rb', line 28

def |(other)
  pipe(other)
end