module Patir::Command

This module defines the interface for a Command object.

It more or less serves the purpose of documenting the interface/contract expected by a class that executes commands and returns their output and exit status.

There is also that bit of functionality that facilitates grouping multiple commands into command sequences

The various methods initialize member variables with meaningful values where needed.

Using the contract means implementing the Command#run method. This method should then set the output, exec_time and status values according to the implementation.

Take a look at ShellCommand and RubyCommand for a couple of practical examples.

It is a good idea to rescue all exceptions. You can then set error to return the exception message.

Attributes

error[W]
exec_time[W]
name[W]
number[RW]
output[W]
status[W]
strategy[RW]

Public Instance Methods

error() click to toggle source

returns the error output for the command

# File lib/patir/command.rb, line 39
def error
  #initialize nil values to something meaningful
  @error||=""
  return @error
end
exec_time() click to toggle source

returns the execution time (duration) for the command

# File lib/patir/command.rb, line 45
def exec_time
  #initialize nil values to something meaningful
  @exec_time||=0
  return @exec_time
end
executed?() click to toggle source

returns false if the command has not been run, alias for run?

# File lib/patir/command.rb, line 76
def executed?
  return false if self.status==:not_executed
  return true
end
name() click to toggle source

returns the commands alias/name

# File lib/patir/command.rb, line 27
def name
  #initialize nil values to something meaningful
  @name||=""
  return @name
end
output() click to toggle source

returns the output of the command

# File lib/patir/command.rb, line 33
def output
  #initialize nil values to something meaningful
  @output||=""
  return @output
end
reset() click to toggle source

clears the status and output of the command.

Call this if you want to pretend that it was never executed

# File lib/patir/command.rb, line 69
def reset
  @exec_time=0
  @output=""
  @error=""
  @status=:not_executed
end
run(context=nil) click to toggle source

executes the command and returns the status of the command.

overwrite this method in classes that include Command

# File lib/patir/command.rb, line 62
def run context=nil
  @status=:success
  return self.status
end
run?() click to toggle source

returns true if the command has been executed

# File lib/patir/command.rb, line 56
def run?
  executed?
end
status() click to toggle source

returns the command status.

valid stati are

:not_executed when the command was not run
:success when the command has finished succesfully
:error when the command has an error
:warning when the command finished without errors, but there where warnings
# File lib/patir/command.rb, line 87
def status
  #initialize nil values to something meaningful
  @status||=:not_executed
  return @status
end
success?() click to toggle source

returns true if the command has finished succesfully

# File lib/patir/command.rb, line 51
def success?
  return true if self.status==:success
  return false
end