Class: TyrantManager::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/tyrant_manager/command.rb

Overview

The Command is the base class for any class to be used as a command

All commands run within the context of an existing TyrantManager location. Before the command starts the current working directory will be inside the tyrant manager’s home directory.

A command has a lifecyle of:

  • instantiation with a TyrantManager instance and an options hash

  • one call to before

  • one call to run

  • one call to after

In case of an exception raised during the lifecycle, the error method will be called. The after method is called no matter what at the end of the lifecycle, even if an error has occurred.

Defined Under Namespace

Classes: CommandNotFoundError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(manager, opts = {}) ⇒ Command

Instantiated and given the tyrant manager instance it is to operate through and a hash of options



33
34
35
36
# File 'lib/tyrant_manager/command.rb', line 33

def initialize( manager, opts = {} )
  @manager = manager
  @options = opts
end

Instance Attribute Details

#managerObject (readonly)

Returns the value of attribute manager.



27
28
29
# File 'lib/tyrant_manager/command.rb', line 27

def manager
  @manager
end

#optionsObject (readonly)

Returns the value of attribute options.



26
27
28
# File 'lib/tyrant_manager/command.rb', line 26

def options
  @options
end

Class Method Details

.command_nameObject



22
23
24
# File 'lib/tyrant_manager/command.rb', line 22

def self.command_name
  name.split("::").last.downcase
end

.find(command) ⇒ Object



104
105
106
107
108
109
# File 'lib/tyrant_manager/command.rb', line 104

def find( command )
  klass = list.find { |klass| klass.command_name == command }
  return klass if klass
  names = list.collect { |c| c.command_name }
  raise CommandNotFoundError, "No command for '#{command}' was found.  Known commands : #{names.join(",")}"
end

.inherited(klass) ⇒ Object



92
93
94
95
# File 'lib/tyrant_manager/command.rb', line 92

def inherited( klass )
  return unless klass.instance_of? Class
  self.list << klass
end

.listObject



97
98
99
100
101
102
# File 'lib/tyrant_manager/command.rb', line 97

def list
  unless defined? @list
    @list = Set.new
  end
  return @list
end

Instance Method Details

#afterObject

call-seq:

cmd.after

called no matter what, after the execution of run() or error() if there was an exception. It is here to allow the cmd to do cleanup work.



76
# File 'lib/tyrant_manager/command.rb', line 76

def after()  nil ; end

#beforeObject

call-seq:

cmd.before

called to allow the command to setup anything post initialization it needs to be able to run.



57
# File 'lib/tyrant_manager/command.rb', line 57

def before() nil ; end

#command_nameObject



38
39
40
# File 'lib/tyrant_manager/command.rb', line 38

def command_name
  self.class.command_name
end

#error(exception) ⇒ Object

call-seq:

cmd.error( exception )

called if there is an exception during the before() or after() calls. It is passed in the exception that was raised.



85
# File 'lib/tyrant_manager/command.rb', line 85

def error(exception)  nil ; end

#loggerObject



42
43
44
# File 'lib/tyrant_manager/command.rb', line 42

def logger
  Logging::Logger[self]
end

#runObject

call-seq:

cmd.run

Yeah, this is where the work should be done.

Raises:

  • (NotImplementedError)


65
66
67
# File 'lib/tyrant_manager/command.rb', line 65

def run() 
  raise NotImplementedError, "The #run method must be implemented"
end