Module: Patir::Command
- Included in:
- RubyCommand, ShellCommand
- Defined in:
- lib/patir/command.rb
Overview
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.
Instance Attribute Summary collapse
-
#error ⇒ Object
returns the error output for the command.
-
#exec_time ⇒ Object
returns the execution time (duration) for the command.
-
#name ⇒ Object
returns the commands alias/name.
-
#number ⇒ Object
Returns the value of attribute number.
-
#output ⇒ Object
returns the output of the command.
-
#status ⇒ Object
returns the command status.
-
#strategy ⇒ Object
Returns the value of attribute strategy.
Instance Method Summary collapse
-
#executed? ⇒ Boolean
returns false if the command has not been run, alias for run?.
-
#reset ⇒ Object
clears the status and output of the command.
-
#run(context = nil) ⇒ Object
executes the command and returns the status of the command.
-
#run? ⇒ Boolean
returns true if the command has been executed.
-
#success? ⇒ Boolean
returns true if the command has finished succesfully.
Instance Attribute Details
#error ⇒ Object
returns the error output for the command
40 41 42 43 44 |
# File 'lib/patir/command.rb', line 40 def error #initialize nil values to something meaningful @error||="" return @error end |
#exec_time ⇒ Object
returns the execution time (duration) for the command
46 47 48 49 50 |
# File 'lib/patir/command.rb', line 46 def exec_time #initialize nil values to something meaningful @exec_time||=0 return @exec_time end |
#name ⇒ Object
returns the commands alias/name
28 29 30 31 32 |
# File 'lib/patir/command.rb', line 28 def name #initialize nil values to something meaningful @name||="" return @name end |
#number ⇒ Object
Returns the value of attribute number.
26 27 28 |
# File 'lib/patir/command.rb', line 26 def number @number end |
#output ⇒ Object
returns the output of the command
34 35 36 37 38 |
# File 'lib/patir/command.rb', line 34 def output #initialize nil values to something meaningful @output||="" return @output end |
#status ⇒ Object
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
88 89 90 91 92 |
# File 'lib/patir/command.rb', line 88 def status #initialize nil values to something meaningful @status||=:not_executed return @status end |
#strategy ⇒ Object
Returns the value of attribute strategy.
26 27 28 |
# File 'lib/patir/command.rb', line 26 def strategy @strategy end |
Instance Method Details
#executed? ⇒ Boolean
returns false if the command has not been run, alias for run?
77 78 79 80 |
# File 'lib/patir/command.rb', line 77 def executed? return false if self.status==:not_executed return true end |
#reset ⇒ Object
clears the status and output of the command.
Call this if you want to pretend that it was never executed
70 71 72 73 74 75 |
# File 'lib/patir/command.rb', line 70 def reset @exec_time=0 @output="" @error="" @status=:not_executed end |
#run(context = nil) ⇒ Object
executes the command and returns the status of the command.
overwrite this method in classes that include Command
63 64 65 66 |
# File 'lib/patir/command.rb', line 63 def run context=nil @status=:success return self.status end |
#run? ⇒ Boolean
returns true if the command has been executed
57 58 59 |
# File 'lib/patir/command.rb', line 57 def run? executed? end |
#success? ⇒ Boolean
returns true if the command has finished succesfully
52 53 54 55 |
# File 'lib/patir/command.rb', line 52 def success? return true if self.status==:success return false end |