Class: Rubish::Executable

Inherits:
Object
  • Object
show all
Defined in:
lib/rubish/stub.rb,
lib/rubish/executable.rb

Overview

Rubish::UnixExecutable < Rubish::Executable Rubish::Command < Rubish::UnixExecutable Rubish::Pipe < Rubish::UnixExecutable

Rubish::Streamer < Rubish::Executable Rubish::Sed < Rubish::Streamer Rubish::Awk < Rubish::Streamer

Rubish::BatchExecutable < Rubish::Executable

Direct Known Subclasses

BatchExecutable, Streamer, UnixExecutable

Defined Under Namespace

Classes: ExecutableIO

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#working_directoryObject (readonly)

Returns the value of attribute working_directory.



117
118
119
# File 'lib/rubish/executable.rb', line 117

def working_directory
  @working_directory
end

Instance Method Details

#awk(a = nil, b = nil, &block) ⇒ Object



125
126
127
128
129
130
131
# File 'lib/rubish/executable.rb', line 125

def awk(a=nil,b=nil,&block)
  if block
    Rubish::Awk.new(self).act(a,b,&block)
  else
    Rubish::Awk.new(self)
  end
end

#cd(dir) ⇒ Object

only changes working directory for this executable



120
121
122
123
# File 'lib/rubish/executable.rb', line 120

def cd(dir)
  @working_directory = dir
  self
end

#each(&block) ⇒ Object



196
197
198
199
# File 'lib/rubish/executable.rb', line 196

def each(&block)
  job = self.each! &block
  job.wait
end

#each!Object



185
186
187
188
189
190
191
192
193
194
# File 'lib/rubish/executable.rb', line 185

def each!
  self.o do |pipe|
    pipe.each_line do |line|
      line.chomp!
      yield(line)
    end
  end
  job = self.exec!
  return job
end

#err(io = nil, &block) ⇒ Object



162
163
164
165
166
# File 'lib/rubish/executable.rb', line 162

def err(io=nil,&block)
  return @__io_err unless io || block
  @__io_err = io || block
  self
end

#err=(io) ⇒ Object



181
182
183
# File 'lib/rubish/executable.rb', line 181

def err=(io)
  @__io_err = io
end

#execObject

stub, see: executable.rb



52
53
54
# File 'lib/rubish/stub.rb', line 52

def exec
  raise "implemented in executable.rb"
end

#exec!Object



145
146
147
# File 'lib/rubish/executable.rb', line 145

def exec!
  raise "abstract"
end

#firstObject



241
242
243
# File 'lib/rubish/executable.rb', line 241

def first
  head(1).first
end

#head(n = 1, &block) ⇒ Object



217
218
219
220
221
222
223
224
225
226
227
# File 'lib/rubish/executable.rb', line 217

def head(n=1,&block)
  raise "n should be greater than 0: #{n}" unless n > 0
  self.map do |l|
    if n == 0
      break
    else
      n -= 1
      block ? block.call(l) : l
    end
  end
end

#i(io = nil, &block) ⇒ Object

methods for io redirection



150
151
152
153
154
# File 'lib/rubish/executable.rb', line 150

def i(io=nil,&block)
  return @__io_in unless io || block
  @__io_in = io || block
  self
end

#i=(io) ⇒ Object



173
174
175
# File 'lib/rubish/executable.rb', line 173

def i=(io)
  @__io_in = io
end

#io(i = nil, o = nil) ⇒ Object



168
169
170
171
# File 'lib/rubish/executable.rb', line 168

def io(i=nil,o=nil)
  i(i); o(o)
  self
end

#lastObject



245
246
247
# File 'lib/rubish/executable.rb', line 245

def last
  tail(1).first
end

#map(&block) ⇒ Object



210
211
212
213
214
215
# File 'lib/rubish/executable.rb', line 210

def map(&block)
  acc = []
  job = self.map!(acc,&block)
  job.wait
  return acc
end

#map!(acc, &block) ⇒ Object

acc is the accumulator passed in by reference, and updated destructively.



202
203
204
205
206
207
208
# File 'lib/rubish/executable.rb', line 202

def map!(acc,&block)
  acc = Array.new unless acc
  job = self.each! do |l|
    acc << (block.nil? ? l : block.call(l))
  end
  return job
end

#o(io = nil, &block) ⇒ Object



156
157
158
159
160
# File 'lib/rubish/executable.rb', line 156

def o(io=nil,&block)
  return @__io_out unless io || block
  @__io_out = io || block
  self
end

#o=(io) ⇒ Object



177
178
179
# File 'lib/rubish/executable.rb', line 177

def o=(io)
  @__io_out = io
end

#sed(a = nil, b = nil, &block) ⇒ Object



133
134
135
136
137
138
139
# File 'lib/rubish/executable.rb', line 133

def sed(a=nil,b=nil,&block)
  if block
    Rubish::Sed.new(self).act(a,b,&block)
  else
    Rubish::Sed.new(self)
  end
end

#tail(n = 1, &block) ⇒ Object



229
230
231
232
233
234
235
236
237
238
239
# File 'lib/rubish/executable.rb', line 229

def tail(n=1,&block)
  raise "n should be greater than 0: #{n}" unless n > 0
  acc = []
  self.each do |l|
    acc << (block ? block.call(l) : l)
    if acc.size > n
      acc.shift
    end
  end
  return acc
end