Class Commands::Dispatchers::Simple
In: commands/dispatchers/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   run   usage  

Included Modules

DispatcherInterface

Constants

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

Public Instance methods

See Commands::Dispatchers::DispatcherInterface#commands.

[Source]

    # File commands/dispatchers/simple.rb, line 84
84:       def commands
85:         cmds = self.class::INFO[:commands]
86:         if !cmds || !cmds.is_a?(Hash)
87:           $logger.error{cmds}
88:           raise 'Please, set the INFO for the dispatcher'
89:         else
90:           return cmds
91:         end
92:       end

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

[Source]

     # File commands/dispatchers/simple.rb, line 96
 96:       def run(cmd, *args)
 97:         $logger.debug {"running command at #{self.class.to_s}"}
 98:         $logger.debug {cmd}
 99:         $logger.debug {args}
100:       
101:         #cmds = self.commands
102:         cmds = self.class::INFO[:commands]
103:       
104:         # do we implement this command?
105:         if !cmds.has_key?(cmd)
106:           raise "#{cmd} not implemented in #{self.class.to_s}"
107:         end
108:       
109:         # is the user requesting help on a specific command?
110:         if ((args.size == 2) && (args[1] == 'help'))
111:           $logger.debug{'the user requested help'}
112:           return self.usage(cmd)
113:         end
114:       
115:         # input validation
116:         if validates?(cmds[cmd],*args)
117:           begin
118:             self.send(cmd, *args)
119:           rescue Errno::ECONNREFUSED
120:             raise $!
121:           end
122:         else
123:           return self.usage(cmd)
124:         end
125:       end

Protected Instance methods

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

[Source]

    # File commands/dispatchers/simple.rb, line 62
62:       def usage(command)
63:         opt = self.class::INFO[:commands][command]
64:       
65:         out = ["Help and usage for: #{command}"]
66:         out << '---------------------------'
67:         out << 'syntax: '
68:         syntax = opt[:syntax].collect do |param|
69:           if param[:required]
70:             "<#{param[:label]}>"
71:           else
72:             "[#{param[:label]}]"
73:           end
74:         end
75:         out << "\t#{command} #{syntax.join(' ')}"
76:         out << 'description: '
77:         out << "\t#{opt[:desc]}"
78:         return out
79:       end

[Validate]