Class: Subcheat::Command
- Inherits:
-
Object
- Object
- Subcheat::Command
- Defined in:
- lib/subcheat/command.rb
Overview
Introduction
A command is a simple combination of a subcommand name and a proc object that generates a subversion command. When invoking subcheat with a subcommand the Command
object with by that name is looked up and executed in the context of current directory (usually a working copy.)
Usage
Command Creation
Commands are created with a special shorthand syntax:
Command.define('subcommand-name') do
# do something useful here
end
Commands are then stored in the Command
class, which can be queried:
Command.on('subcommand-name')
The returning subcommand can then be executed given a specific Subversion context (an instance of Subcheat::Svn
):
Command.on('subcommand-name').call(Svn.new)
Writing Commands
Because commands get executed in a <ttr>Subcheat::SVN</tt> context, they have access to all its methods and instance variables. See Subcheat::SVN
for more information.
Note that a command should always return either a Subversion command statement as a string (e.g. “svn status”). If it returns nothing, nothing will be done.
Commands can be stored in the lib/subcheat/commands
directory, so they will be automatically loaded together with this class. Filenames are irrelevant.
Exceptions
When something goes wrong, commands can raise a CommandException
exception. When querying Command for a non-existant subcommand a NoSuchCommand
exception will be raised.
Class Attribute Summary collapse
-
.commands ⇒ Object
readonly
List of all available commands.
Instance Attribute Summary collapse
-
#execute ⇒ Object
readonly
Should the output be executed as a shell command, or printed?.
-
#subcommand ⇒ Object
readonly
Name of the subcommand to which this command should respond.
Class Method Summary collapse
-
.define(*args, &block) ⇒ Object
:call-seq: define(subcommand, &block).
-
.on(subcommand) ⇒ Object
Query for a
Command
object by the given subcommand name.
Instance Method Summary collapse
-
#call(svn) ⇒ Object
Invoke the
Command
‘s method to generate and return a subversion CLI statement. -
#initialize(subcommand, execute = true, &block) ⇒ Command
constructor
:nodoc:.
Constructor Details
#initialize(subcommand, execute = true, &block) ⇒ Command
:nodoc:
89 90 91 |
# File 'lib/subcheat/command.rb', line 89 def initialize(subcommand, execute = true, &block) #:nodoc: @subcommand, @execute, @method = subcommand, execute, block end |
Class Attribute Details
.commands ⇒ Object (readonly)
List of all available commands
59 60 61 |
# File 'lib/subcheat/command.rb', line 59 def commands @commands end |
Instance Attribute Details
#execute ⇒ Object (readonly)
Should the output be executed as a shell command, or printed?
52 53 54 |
# File 'lib/subcheat/command.rb', line 52 def execute @execute end |
#subcommand ⇒ Object (readonly)
Name of the subcommand to which this command should respond.
49 50 51 |
# File 'lib/subcheat/command.rb', line 49 def subcommand @subcommand end |
Class Method Details
.define(*args, &block) ⇒ Object
:call-seq: define(subcommand, &block)
Shortcut method to creating and registering a new Command
object.
This will instantiate a new Command
with the given subcommand name, and the given block as its method to execute when invoked.
Example usage:
Command.define('nuke') do
exec "rm -Rf"
end
74 75 76 |
# File 'lib/subcheat/command.rb', line 74 def define(*args, &block) @commands << new(*args, &block) end |
.on(subcommand) ⇒ Object
Query for a Command
object by the given subcommand name.
This will return either a Command
object to be invoked, or it will raise a NoSuchCommand
exception.
82 83 84 85 86 |
# File 'lib/subcheat/command.rb', line 82 def on(subcommand) command = @commands.select { |c| c.subcommand == subcommand }.first raise NoSuchCommand if command.nil? command end |
Instance Method Details
#call(svn) ⇒ Object
Invoke the Command
‘s method to generate and return a subversion CLI statement.
This requires an instance of Subcheat::Svn
to be passed in, which will be used as context to execute the method in.
98 99 100 101 |
# File 'lib/subcheat/command.rb', line 98 def call(svn) Subcheat::Runner.perform_run = false unless execute svn.instance_eval(&@method) end |