Class: Warp::Dir::Commander
- Includes:
- Singleton
- Defined in:
- lib/warp/dir/commander.rb
Instance Attribute Summary collapse
-
#command_map ⇒ Object
Returns the value of attribute command_map.
-
#commands ⇒ Object
readonly
Returns the value of attribute commands.
Instance Method Summary collapse
- #find(command_name) ⇒ Object
-
#initialize ⇒ Commander
constructor
A new instance of Commander.
- #installed_commands ⇒ Object
- #lookup(command_name) ⇒ Object
- #register(command) ⇒ Object
- #reindex! ⇒ Object
- #run(command_name, *args) ⇒ Object
- #validate! ⇒ Object
Constructor Details
#initialize ⇒ Commander
Returns a new instance of Commander.
13 14 15 16 |
# File 'lib/warp/dir/commander.rb', line 13 def initialize @commands ||= Set.new # a pre-caution, normally it would already by defined by now @command_map = {} end |
Instance Attribute Details
#command_map ⇒ Object
Returns the value of attribute command_map.
11 12 13 |
# File 'lib/warp/dir/commander.rb', line 11 def command_map @command_map end |
#commands ⇒ Object (readonly)
Returns the value of attribute commands.
10 11 12 |
# File 'lib/warp/dir/commander.rb', line 10 def commands @commands end |
Instance Method Details
#find(command_name) ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/warp/dir/commander.rb', line 32 def find(command_name) command = lookup(command_name) if command.nil? raise ::Warp::Dir::Errors::InvalidCommand.new(command_name) end command end |
#installed_commands ⇒ Object
23 24 25 |
# File 'lib/warp/dir/commander.rb', line 23 def installed_commands @commands.map(&:command_name) end |
#lookup(command_name) ⇒ Object
27 28 29 30 |
# File 'lib/warp/dir/commander.rb', line 27 def lookup(command_name) reindex! command_map[command_name] end |
#register(command) ⇒ Object
18 19 20 21 |
# File 'lib/warp/dir/commander.rb', line 18 def register(command) @commands << command if command self end |
#reindex! ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/warp/dir/commander.rb', line 46 def reindex! commands.each do |command| if command.respond_to?(:aliases) command.aliases.each do |an_alias| if self.command_map[an_alias] && !self.command_map[an_alias] == command raise Warp::Dir::Errors::InvalidCommand.new("Duplicate alias for command #{command}") end self.command_map[an_alias] = command end end self.command_map[command.command_name] = command end self end |
#run(command_name, *args) ⇒ Object
40 41 42 43 44 |
# File 'lib/warp/dir/commander.rb', line 40 def run(command_name, *args) cmd = find command_name raise ::Warp::Dir::Errors::InvalidCommand.new(command_name) unless cmd.is_a?(warp::Dir::Command) cmd.new(*args).run end |
#validate! ⇒ Object
61 62 63 64 65 66 67 68 |
# File 'lib/warp/dir/commander.rb', line 61 def validate! self.commands.delete_if do |subclass| if !subclass.respond_to?(:abstract_class?) && !subclass.method_defined?(:run) raise ::Warp::Dir::Errors::InvalidCommand.new(subclass) end subclass.respond_to?(:abstract_class?) || !subclass.method_defined?(:run) end end |