Class Core::Extensions::Simple
In: core/extensions/simple.rb
Parent: Object

This class is designed as the easiest way to implement the Commands::Dispatchers::DispatcherInterface. Classes inheriting from this dispatcher will only need to have the desired funcionality and provide some information regarding this functionality provided.

See Commands::Dispatchers::DisparcherInterface#commands for further information regarding the way this details must be provided.

Methods

commands   new   run   usage  

Included Modules

Interfaces::Extension

Constants

INFO = { :commands => nil }  
=============================================== private methods

Public Class methods

Store the controller, just in case the extension wants to use any service provided by other ServiceProviders.

[Source]

# File core/extensions/simple.rb, line 129
      def initialize(params={})
        @controller = params.fetch(:controller, nil)
      end

Public Instance methods

See Commands::Dispatchers::DispatcherInterface#commands.

[Source]

# File core/extensions/simple.rb, line 84
      def commands
        cmds = self.class::INFO[:commands]
        if !cmds || !cmds.is_a?(Hash)
          $logger.error{cmds}
          raise 'Please, set the INFO for the dispatcher'
        else
          return cmds
        end
      end

Run a given command with the specified parameters. See Commands::Dispatchers::DispatcherInterface#run.

[Source]

# File core/extensions/simple.rb, line 96
      def run(cmd, *args)
        $logger.debug {"running command at #{self.class.to_s}"}
        $logger.debug {cmd}
        $logger.debug {args}
      
        #cmds = self.commands
        cmds = self.class::INFO[:commands]
      
        # do we implement this command?
        if !cmds.has_key?(cmd)
          raise "#{cmd} not implemented in #{self.class.to_s}"
        end
      
        # is the user requesting help on a specific command?
        if ((args.size == 2) && (args[1] == 'help'))
          $logger.debug{'the user requested help'}
          return self.usage(cmd)
        end
      
        # input validation
        if validates?(cmds[cmd],*args)
          begin
            self.send(cmd, *args)
          rescue Errno::ECONNREFUSED
            raise $!
          end
        else
          return self.usage(cmd)
        end
      end

Protected Instance methods

Provide help with the syntax of the different commands provided by this module.

[Source]

# File core/extensions/simple.rb, line 59
      def usage(command)
        opt = self.class::INFO[:commands][command]
      
        out = ["Help and usage for: #{command}"]
        out << '---------------------------'
        out << 'syntax: '
        syntax = opt[:syntax].collect do |param|
          if param[:required]
            "<#{param[:label]}>"
          else
            "[#{param[:label]}]"
          end
        end
        out << "\t#{command} #{syntax.join(' ')}"
        out << 'description: '
        out << "\t#{opt[:desc]}\n"
        out << opt[:syntax].collect do |param| 
          "\t#{param[:label]}:\n\t\t#{param[:desc]}" if (param.key?(:desc))
        end
        return out
      end

[Validate]