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



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

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

Instance Attribute Details

#managerObject (readonly)

Returns the value of attribute manager.



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

def manager
  @manager
end

#optionsObject (readonly)

Returns the value of attribute options.



31
32
33
# File 'lib/tyrant_manager/command.rb', line 31

def options
  @options
end

Class Method Details

.command_nameObject



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

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

.find(command) ⇒ Object



109
110
111
112
113
114
# File 'lib/tyrant_manager/command.rb', line 109

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



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

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

.listObject



102
103
104
105
106
107
# File 'lib/tyrant_manager/command.rb', line 102

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.



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

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.



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

def before() nil ; end

#command_nameObject



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

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.



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

def error(exception)  nil ; end

#loggerObject



47
48
49
# File 'lib/tyrant_manager/command.rb', line 47

def logger
  Logging::Logger[self]
end

#runObject

call-seq:

cmd.run

Yeah, this is where the work should be done.

Raises:

  • (NotImplementedError)


70
71
72
# File 'lib/tyrant_manager/command.rb', line 70

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