Class: ObjectiveCommand::Datas::Data

Inherits:
Object
  • Object
show all
Defined in:
lib/objective_command/datas/data.rb

Overview

A CommandData instance contains all information returned by a Command#run:

* It's exit status:
  - Exit number.
  - Signals
  - ...

* It's outputs:
  - What it writes on stdout.
  - stderr.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeData

Returns a new instance of Data.



32
33
34
35
36
# File 'lib/objective_command/datas/data.rb', line 32

def initialize
  @open_mode = :w
  @status, @pid = nil, nil
  @@datas << self
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *a, &b) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/objective_command/datas/data.rb', line 59

def method_missing ( meth, *a, &b )
  if output.respond_to?(meth)
    output.send(meth, *a, &b)
  else
    super
  end
end

Instance Attribute Details

#errorObject

Returns the value of attribute error.



26
27
28
# File 'lib/objective_command/datas/data.rb', line 26

def error
  @error
end

#inputObject

Returns the value of attribute input.



24
25
26
# File 'lib/objective_command/datas/data.rb', line 24

def input
  @input
end

#open_modeObject

Returns the value of attribute open_mode.



28
29
30
# File 'lib/objective_command/datas/data.rb', line 28

def open_mode
  @open_mode
end

#outputObject

Returns the value of attribute output.



25
26
27
# File 'lib/objective_command/datas/data.rb', line 25

def output
  @output
end

#pidObject

Returns the value of attribute pid.



23
24
25
# File 'lib/objective_command/datas/data.rb', line 23

def pid
  @pid
end

#statusObject

Returns the value of attribute status.



27
28
29
# File 'lib/objective_command/datas/data.rb', line 27

def status
  @status
end

Class Method Details

.clean_allObject



165
166
167
# File 'lib/objective_command/datas/data.rb', line 165

def self.clean_all
  @@datas.dup.each { |data| data.clean }
end

Instance Method Details

#cleanObject



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/objective_command/datas/data.rb', line 139

def clean
  return unless @status.nil?
  return if @pid.nil?
  if Process.waitpid(@pid, Process::WNOHANG)
    @status = $?
    @@datas.delete(self)
  else
    kill
  end
  [input, output, error].each do |stream|
    next if stream.nil? or stream.is_a? IO
    if stream.is_a? File
      stream.close unless stream.closed?
      stream = stream.path.to_path
    end
    if stream.is_a? Pathname
      stream.unlink if stream.exist? and stream.temp?
    elsif stream.is_a? String
    elsif stream.respond_to? :clean
      stream.clean
    else
      raise ArgumentError, "must at least respond to :clean (#{stream})"
    end
  end
end

#display(out = STDOUT, err = STDERR) ⇒ Object



68
69
70
71
72
# File 'lib/objective_command/datas/data.rb', line 68

def display ( out=STDOUT, err=STDERR )
  waitpid
  out.print @output.read
  err.print @error.read
end

#exitObject



105
106
107
108
109
110
111
# File 'lib/objective_command/datas/data.rb', line 105

def exit
  if status.signaled?
    128 + status.termsig # Common exit status returned by sh
  else
    status.exitstatus
  end
end

#kill(sig = 'KILL') ⇒ Object



134
135
136
137
# File 'lib/objective_command/datas/data.rb', line 134

def kill ( sig='KILL' )
  Process.kill sig, @pid if @status.nil? and not @pid.nil?
  waitpid
end

#to_yaml_stringObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/objective_command/datas/data.rb', line 115

def to_yaml_string
  suppress(IOError) { out = output.read }
  suppress(IOError) { err = error.read }
  out ||= ''
  err ||= ''

  msg = []
  msg << "exit: #{status.exitstatus}" unless status.nil?
  unless out.empty?
    out.gsub!(/^/, '    ')
    msg << "output: |\n  #{out}"
  end
  unless err.empty?
    err.gsub!(/^/, '    ')
    msg << "error: |\n  #{err}"
  end
  "\n" + msg.join("\n")
end

#waitpidObject



39
40
41
42
43
44
45
46
# File 'lib/objective_command/datas/data.rb', line 39

def waitpid
  return @status unless @status.nil?
  return if @pid.nil?
  Process.waitpid(@pid)
  @status = $?
  @@datas.delete(self)
  self
end

#|(rhs) ⇒ Object

FIXME CHECK ME



49
50
51
52
53
54
55
56
# File 'lib/objective_command/datas/data.rb', line 49

def | ( rhs ) # FIXME CHECK ME
  raise unless rhs.is_a? Commands::Command
  case output
  when Pathname then rhs < output
  when File     then rhs < output.path.to_path
  else raise "Unexpected class #{rhs.class}"
  end
end