Class: Chitin::Pipe

Inherits:
Object show all
Includes:
Runnable
Defined in:
lib/chitin/commands/pipe.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Runnable

#<, #>, #>>, #[], #^, #bg!, #bg?, #fg?

Constructor Details

#initialize(*parts) ⇒ Pipe

Returns a new instance of Pipe.



8
9
10
11
12
13
14
# File 'lib/chitin/commands/pipe.rb', line 8

def initialize(*parts)
  @parts = parts

  super()
  
  link_all
end

Instance Attribute Details

#partsObject

Returns the value of attribute parts.



5
6
7
# File 'lib/chitin/commands/pipe.rb', line 5

def parts
  @parts
end

#pidsObject

Returns the value of attribute pids.



6
7
8
# File 'lib/chitin/commands/pipe.rb', line 6

def pids
  @pids
end

Instance Method Details

#errObject



94
95
96
# File 'lib/chitin/commands/pipe.rb', line 94

def err
  @parts.last[:err]
end

#err=(other) ⇒ Object



97
# File 'lib/chitin/commands/pipe.rb', line 97

def err=(other); @parts.last[:set_err, other]; end

#inObject



84
85
86
# File 'lib/chitin/commands/pipe.rb', line 84

def in
  @parts.first[:in]
end

#in=(other) ⇒ Object



87
# File 'lib/chitin/commands/pipe.rb', line 87

def in=(other); @parts.first[:set_in, other]; end


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/chitin/commands/pipe.rb', line 27

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 everything together



17
18
19
20
21
22
23
24
25
# File 'lib/chitin/commands/pipe.rb', line 17

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

#outObject



89
90
91
# File 'lib/chitin/commands/pipe.rb', line 89

def out
  @parts.last[:out]
end

#out=(other) ⇒ Object



92
# File 'lib/chitin/commands/pipe.rb', line 92

def out=(other); @parts.last[:set_out, other]; end

#raw_runObject

Run them all but let the last run return its value



54
55
56
57
58
59
60
# File 'lib/chitin/commands/pipe.rb', line 54

def raw_run
  parts[0..-2].each {|part| part[:run] }
  result = parts.last[:raw_run]
  reset
  
  result
end

#resetObject



68
69
70
71
72
73
# File 'lib/chitin/commands/pipe.rb', line 68

def reset
  parts.map {|part| part[:reset] }
  link_all # we have to refresh the pipes connecting everything
  
  self
end

#returningObject



103
104
105
# File 'lib/chitin/commands/pipe.rb', line 103

def returning
  parts.last[:returning]
end

#runObject

Like raw_run, but meant to be used in chaining.



46
47
48
49
50
51
# File 'lib/chitin/commands/pipe.rb', line 46

def run
  parts.each {|part| part[:run] }
  reset
  
  self
end

#to_sObject



99
100
101
# File 'lib/chitin/commands/pipe.rb', line 99

def to_s
  "#{self.in} > #{parts.map {|p| p[:to_s] }.join ' | '} > #{self.out}"
end

#waitObject



62
63
64
65
66
# File 'lib/chitin/commands/pipe.rb', line 62

def wait
  parts.each {|part| part[:wait] }
  
  self
end

#|(other) ⇒ Object



75
76
77
78
79
80
81
82
# File 'lib/chitin/commands/pipe.rb', line 75

def |(other)
  raise "#{other.class} needs to include Runnable" unless Runnable === other
  
  link parts.last, other
  parts << other
  
  self
end