Class Core::Extensions::Namespace
In: core/extensions/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

Interfaces::Extension

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 core/extensions/namespace.rb, line 56
      def commands
        return {
          self.class::INFO[:namespace] => {
            :desc => self.class::INFO[:description],
            :syntax => [
            {
              :required => true,
              :label=>'option', 
              :regexp=> /\w+/
            }
            ]#multiple params,at least one
          }
        }
      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 core/extensions/namespace.rb, line 80
      def run(cmd, *args)
        $logger.debug {"namespace running command at #{self.class.to_s}"}
        $logger.debug {cmd}
        $logger.debug {args}
        
        if args.size == 1
          out = [ "#{self.class::INFO[:namespace].capitalize} module. List of registered commands"]
          out << '---------------------------'
          self.class::INFO[:commands].each do |cmd, opts|
            out << "#{cmd}:\t#{opts[:desc]}" 
          end
          return out
        else
          #first get rid of the first argument, the namespace that is not
          #needed anymore
          args.shift
          super(args.first,*args)
        end
      end

[Validate]