Class Commands::Dispatchers::Namespace
In: commands/dispatchers/namespace.rb
Parent: Simple

Implementation of the Dispatcher interface for modules that define a namespace. A namesepace is the way modules can group commands.

Say for example we want the user to be able to type: add host but also add protocol. Since both actions are related to adding something we want them to be handled by the same dispatcher, so we inherit from Namespace and set the :namespace setting to "add". Then we will need to implement the two methods (host, protocol) as if we were working with a Simple dispatcher.

Appart from the :commands^key described in the DispatcherInterface for the INFO constant, modules inheriting from Namespace should provide two new keys:

 INFO = {
   :namespace => '',
   :description => '',
   :commands => {}
 }

Where:

  [+namespace+] is the name that groups all the actions provided by the module.
  [+description+] is a general description of the actions provided.
  [+commands+] has the syntax described in the DispatcherInterface.

Methods

commands   run  

Included Modules

DispatcherInterface

Constants

INFO = { :namespace => 'This dispatcher should override the :namespace value', :description => 'This dispatcher should override the :description value', :commands => nil

Public Instance methods

Internally calls namespace. NamespaceDispatcher only listens for a method: only user requests that start with the value provided in INFO[:namespace] are attended.

[Source]

    # File commands/dispatchers/namespace.rb, line 59
59:       def commands
60:         return {
61:           self.class::INFO[:namespace] => {
62:             :desc => self.class::INFO[:description],
63:             :syntax => [
64:             {
65:               :required => true,
66:               :label=>'option', 
67:               :regexp=> /\w+/
68:             }
69:             ]#multiple params,at least one
70:           }
71:         }
72:       end

Provides the internal logic necessary to execute commands grouped under the same namespace, thus enabling subclases to focus only on the command implementation and not in the logic of deciding which command to run.

Only user lines that start with INFO[:namespace] will be routed here. This method checks the subsequent parameters to determine if the command requested is implemented. If it is, delegates the command execution to the superclass (Simple). If it is not, a list of valid commands is returned.

[Source]

     # File commands/dispatchers/namespace.rb, line 83
 83:       def run(cmd, *args)
 84:         $logger.debug {"namespace running command at #{self.class.to_s}"}
 85:         $logger.debug {cmd}
 86:         $logger.debug {args}
 87:         
 88:         if args.size == 1
 89:           out = [ "#{self.class::INFO[:namespace].capitalize} module. List of registered commands"]
 90:           out << '---------------------------'
 91:           self.class::INFO[:commands].each do |cmd, opts|
 92:             out << "#{cmd}:\t#{opts[:desc]}" 
 93:           end
 94:           return out
 95:         else
 96:           #first get rid of the first argument, the namespace that is not
 97:           #needed anymore
 98:           args.shift
 99:           super(args.first,*args)
100:         end
101:       end

[Validate]