Class: Coque::Pipeline
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!
#<, #>, #>=, #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
#commands ⇒ Object
Returns the value of attribute commands.
6
7
8
|
# File 'lib/coque/pipeline.rb', line 6
def commands
@commands
end
|
Instance Method Details
#clone ⇒ Object
11
12
13
|
# File 'lib/coque/pipeline.rb', line 11
def clone
self.class.new(commands)
end
|
#get_result ⇒ Object
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
|
#stitch ⇒ Object
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
if commands.first.stdin.nil?
start_r, start_w = IO.pipe
start_w.close
commands.first.stdin = start_r
end
commands.each_cons(2) do |left, right|
read, write = IO.pipe
left.stdout = write
right.stdin = read
end
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_s ⇒ Object
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
|