Class: Chitin::Pipe
- Includes:
- Runnable
- Defined in:
- lib/chitin/commands/pipe.rb
Instance Attribute Summary collapse
-
#parts ⇒ Object
Returns the value of attribute parts.
-
#pids ⇒ Object
Returns the value of attribute pids.
Instance Method Summary collapse
- #err ⇒ Object
- #err=(other) ⇒ Object
- #in ⇒ Object
- #in=(other) ⇒ Object
-
#initialize(*parts) ⇒ Pipe
constructor
A new instance of Pipe.
- #link(left, right) ⇒ Object
-
#link_all ⇒ Object
link everything together.
- #out ⇒ Object
- #out=(other) ⇒ Object
-
#raw_run ⇒ Object
Run them all but let the last run return its value.
- #reset ⇒ Object
- #returning ⇒ Object
-
#run ⇒ Object
Like raw_run, but meant to be used in chaining.
- #to_s ⇒ Object
- #wait ⇒ Object
- #|(other) ⇒ Object (also: #<=>)
Methods included from Runnable
Constructor Details
#initialize(*parts) ⇒ Pipe
Returns a new instance of Pipe.
8 9 10 11 12 |
# File 'lib/chitin/commands/pipe.rb', line 8 def initialize(*parts) @parts = parts link_all end |
Instance Attribute Details
#parts ⇒ Object
Returns the value of attribute parts.
5 6 7 |
# File 'lib/chitin/commands/pipe.rb', line 5 def parts @parts end |
#pids ⇒ Object
Returns the value of attribute pids.
6 7 8 |
# File 'lib/chitin/commands/pipe.rb', line 6 def pids @pids end |
Instance Method Details
#err ⇒ Object
93 94 95 |
# File 'lib/chitin/commands/pipe.rb', line 93 def err @parts.last[:err] end |
#err=(other) ⇒ Object
96 |
# File 'lib/chitin/commands/pipe.rb', line 96 def err=(other); @parts.last[:set_err, other]; end |
#in ⇒ Object
83 84 85 |
# File 'lib/chitin/commands/pipe.rb', line 83 def in @parts.first[:in] end |
#in=(other) ⇒ Object
86 |
# File 'lib/chitin/commands/pipe.rb', line 86 def in=(other); @parts.first[:set_in, other]; end |
#link(left, right) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/chitin/commands/pipe.rb', line 25 def link(left, right) # The pipe that we use to connect the dots r, w = IO.pipe # since left will want to write to STDOUT, we have to give it # something else it can write to. left > w # same thing for right, but with reading r > right # right will try to read from r until w is closed. # when w is closed, right will finish processing. # if we don't close this, right will never return w.close r.close # and we close r so that we don't have any open useless handles end |
#link_all ⇒ Object
link everything together
15 16 17 18 19 20 21 22 23 |
# File 'lib/chitin/commands/pipe.rb', line 15 def link_all # move from left to right through the list # link the left one to the right one parts[1..-1].inject parts.first do |left, right| link left, right # return this, and shift the whole process one to the right right end end |
#out ⇒ Object
88 89 90 |
# File 'lib/chitin/commands/pipe.rb', line 88 def out @parts.last[:out] end |
#out=(other) ⇒ Object
91 |
# File 'lib/chitin/commands/pipe.rb', line 91 def out=(other); @parts.last[:set_out, other]; end |
#raw_run ⇒ Object
Run them all but let the last run return its value
52 53 54 55 56 57 58 |
# File 'lib/chitin/commands/pipe.rb', line 52 def raw_run parts[0..-2].each {|part| part[:run] } result = parts.last[:raw_run] reset result end |
#reset ⇒ Object
66 67 68 69 70 71 |
# File 'lib/chitin/commands/pipe.rb', line 66 def reset parts.map {|part| part[:reset] } link_all # we have to refresh the pipes connecting everything self end |
#returning ⇒ Object
102 103 104 105 106 107 108 |
# File 'lib/chitin/commands/pipe.rb', line 102 def returning if [StringMethod, Proc].include?(@parts.last[:class]) :ruby else :io end end |
#run ⇒ Object
Like raw_run, but meant to be used in chaining.
44 45 46 47 48 49 |
# File 'lib/chitin/commands/pipe.rb', line 44 def run parts.each {|part| part[:run] } reset self end |
#to_s ⇒ Object
98 99 100 |
# File 'lib/chitin/commands/pipe.rb', line 98 def to_s "#{self.in} > #{parts.map {|p| p[:to_s] }.join ' | '} > #{self.out}" end |
#wait ⇒ Object
60 61 62 63 64 |
# File 'lib/chitin/commands/pipe.rb', line 60 def wait parts.each {|part| part[:wait] } self end |