Class: Cmd
- Inherits:
-
Object
- Object
- Cmd
- Defined in:
- lib/mfe.rb
Overview
Represents an individual command
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
Class Method Summary collapse
-
.load(filename) ⇒ Object
Turns a filename into a command! Amazing! Pass the full path.
Instance Method Summary collapse
-
#expand(argv) ⇒ Object
Turns the command into a line of shell, fit to be exec()‘d or “’d.
-
#initialize(name, cmd) ⇒ Cmd
constructor
Creates a new command.
-
#run(argv) ⇒ Object
run
runs the command, returning the output. -
#save(dirname) ⇒ Object
save
, saves the command to a file. - #to_hash ⇒ Object
-
#to_s ⇒ Object
to_s
simply returns the command string.
Constructor Details
#initialize(name, cmd) ⇒ Cmd
Creates a new command.
220 221 222 223 |
# File 'lib/mfe.rb', line 220 def initialize(name, cmd) @name = name @cmd = cmd end |
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
203 204 205 |
# File 'lib/mfe.rb', line 203 def name @name end |
Class Method Details
.load(filename) ⇒ Object
Turns a filename into a command! Amazing! Pass the full path.
206 207 208 209 210 |
# File 'lib/mfe.rb', line 206 def self.load(filename) obj = JSON.parse(File.read(filename)) new(File.basename(filename), obj['cmd']) end |
Instance Method Details
#expand(argv) ⇒ Object
Turns the command into a line of shell, fit to be exec()‘d or “’d.
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/mfe.rb', line 235 def (argv) argv = argv.map(&:shell_escape) @cmd.map { |this_arg| a = this_arg.dup a.gsub!(/\bARGS\b/, argv.join(' ')) a.gsub!(/\bARGEVAL\[(.*)\]/) { |m| argeval = $1 argv.map! { |frozen_arg| arg = frozen_arg.dup eval(argeval, binding).to_s } '' } a.gsub!(/\bEVAL\[(.*)\]/) { |m| eval($1, binding).to_s } a }.join(' ') end |
#run(argv) ⇒ Object
run
runs the command, returning the output.
256 257 258 |
# File 'lib/mfe.rb', line 256 def run(argv) `#{ argv}` end |
#save(dirname) ⇒ Object
save
, saves the command to a file.
213 214 215 216 217 |
# File 'lib/mfe.rb', line 213 def save(dirname) File.open(File.join(dirname, @name), 'w') { |f| f.puts JSON.pretty_unparse(to_hash) } end |
#to_hash ⇒ Object
230 231 232 |
# File 'lib/mfe.rb', line 230 def to_hash {name: @name, cmd: @cmd} end |
#to_s ⇒ Object
to_s
simply returns the command string.
226 227 228 |
# File 'lib/mfe.rb', line 226 def to_s @cmd.join(' ') end |