Class: Commands::Pipe

Inherits:
Command show all
Defined in:
lib/commands/pipe.rb

Instance Attribute Summary

Attributes inherited from Command

#args, #command, #dir, #open_mode

Instance Method Summary collapse

Methods inherited from Command

#+, #<, #<<, #>, #>>, [], #arg_string, #exec, #expand, #fork, #instanciate_args, #instanciate_args!, #popen, #ruby, #run_with_options, #sh, #sh!, #sh_args, #sys, #system, #to_a, #to_cmd, #whereis, #|

Constructor Details

#initialize(*cmds) ⇒ Pipe

Returns a new instance of Pipe.



32
33
34
# File 'lib/commands/pipe.rb', line 32

def initialize ( *cmds )
  @cmds = cmds.flatten.map { |x| x.dup }
end

Instance Method Details

#[](*args) ⇒ Object



91
92
93
94
95
# File 'lib/commands/pipe.rb', line 91

def [] ( *args )
  new_cmds = @cmds.dup
  new_cmds[-1] = new_cmds.last[*args]
  Pipe.new(*new_cmds)
end

#errorObject



79
80
81
# File 'lib/commands/pipe.rb', line 79

def error
  @cmds.last.error
end

#error=(arg) ⇒ Object



75
76
77
# File 'lib/commands/pipe.rb', line 75

def error= ( arg )
  @cmds.last.error = arg
end

#inputObject



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

def input
  @cmds.first.input
end

#input=(arg) ⇒ Object



59
60
61
# File 'lib/commands/pipe.rb', line 59

def input= ( arg )
  @cmds.first.input = arg
end

#outputObject



71
72
73
# File 'lib/commands/pipe.rb', line 71

def output
  @cmds.last.output
end

#output=(arg) ⇒ Object



67
68
69
# File 'lib/commands/pipe.rb', line 67

def output= ( arg )
  @cmds.last.output = arg
end

#run(runner = @runner, *a) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/commands/pipe.rb', line 36

def run ( runner=@runner, *a )
  datas = Datas::Composite.new
  runner = runner.dup
  runner.hook_trigger :display_command, self
  runner.disable_hook :display_command
  runner.disable_hook :waitpid
  ([nil] + @cmds).zip(@cmds + [nil]).each do |cmd1, cmd2|
    next if cmd1.nil? or cmd2.nil?
    rd, wr = IO.pipe
    cmd1_runner = runner.dup
    cmd1_runner.disable_hook :waitpid
    cmd1_runner.subscribe_hook(:son) { rd.close }
    cmd1.output = wr
    cmd2.input = rd
    datas << cmd1.run(cmd1_runner)
    wr.close
  end
  runner = runner.dup
  datas << @cmds.last.run(runner, *a)
  datas.waitpid if runner.is_a? Runners::System
  datas
end

#to_sObject



106
107
108
# File 'lib/commands/pipe.rb', line 106

def to_s
  to_sh
end

#to_shObject

Conversion methods



101
102
103
104
# File 'lib/commands/pipe.rb', line 101

def to_sh
  strs = @cmds.map { |cmd| "(#{cmd.to_sh})" }
  "#{strs.join(' | ')}"
end