Class: ObjectiveCommand::Commands::Pipe

Inherits:
Command show all
Defined in:
lib/objective_command/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, #freeze, #inspect_for_unified_matchers, #instanciate_args, #instanciate_args!, #popen, #ruby, #run_with_options, #sh, #sh!, #sh_args, #sys, #system, #to_a, #to_ocmd, #whereis, #|

Constructor Details

#initialize(*cmds) ⇒ Pipe

Returns a new instance of Pipe.



34
35
36
# File 'lib/objective_command/commands/pipe.rb', line 34

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

Instance Method Details

#[](*args) ⇒ Object



96
97
98
99
100
# File 'lib/objective_command/commands/pipe.rb', line 96

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

#errorObject



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

def error
  @cmds.last.error
end

#error=(arg) ⇒ Object



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

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

#inputObject



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

def input
  @cmds.first.input
end

#input=(arg) ⇒ Object



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

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

#outputObject



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

def output
  @cmds.last.output
end

#output=(arg) ⇒ Object



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

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

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



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/objective_command/commands/pipe.rb', line 38

def run ( runner=@runner, *a )
  if runner.nil?
    raise ArgumentError, "need a runner not: #{runner.inspect}"
  end
  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



111
112
113
# File 'lib/objective_command/commands/pipe.rb', line 111

def to_s
  to_sh
end

#to_shObject

Conversion methods



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

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