Class: GitCommander::Registry Abstract
- Inherits:
-
Object
- Object
- GitCommander::Registry
- Defined in:
- lib/git_commander/registry.rb
Overview
Manages available GitCommander commands
Defined Under Namespace
Classes: CommandNotFound, LoadError
Instance Attribute Summary collapse
-
#commands ⇒ Object
Returns the value of attribute commands.
-
#name ⇒ Object
Returns the value of attribute name.
-
#plugins ⇒ Object
Returns the value of attribute plugins.
Instance Method Summary collapse
-
#find(command_name) ⇒ GitCommander::Command, #run
Looks up a command in the registry.
- #find_plugin(plugin_name) ⇒ Object
-
#initialize ⇒ Registry
constructor
A new instance of Registry.
-
#load(loader, *args) ⇒ Object
Adds command(s) to the registry using the given loader.
-
#register(command_name, **options, &block) ⇒ Object
Adds a command to the registry.
-
#register_command(command) ⇒ Object
Adds a pre-built command to the registry.
-
#register_plugin(plugin) ⇒ Object
Adds a pre-built Plugin to the registry.
Constructor Details
#initialize ⇒ Registry
Returns a new instance of Registry.
16 17 18 19 |
# File 'lib/git_commander/registry.rb', line 16 def initialize @commands = {} @plugins = {} end |
Instance Attribute Details
#commands ⇒ Object
Returns the value of attribute commands.
14 15 16 |
# File 'lib/git_commander/registry.rb', line 14 def commands @commands end |
#name ⇒ Object
Returns the value of attribute name.
14 15 16 |
# File 'lib/git_commander/registry.rb', line 14 def name @name end |
#plugins ⇒ Object
Returns the value of attribute plugins.
14 15 16 |
# File 'lib/git_commander/registry.rb', line 14 def plugins @plugins end |
Instance Method Details
#find(command_name) ⇒ GitCommander::Command, #run
Looks up a command in the registry
72 73 74 75 76 77 78 |
# File 'lib/git_commander/registry.rb', line 72 def find(command_name) GitCommander.logger.debug "[#{logger_tag}] looking up command: #{command_name.inspect}" command = commands[command_name.to_s.to_sym] raise CommandNotFound, "[#{logger_tag}] #{command_name} does not exist in the registry" if command.nil? command end |
#find_plugin(plugin_name) ⇒ Object
80 81 82 83 |
# File 'lib/git_commander/registry.rb', line 80 def find_plugin(plugin_name) GitCommander.logger.debug "[#{logger_tag}] looking up plugin: #{plugin_name.inspect}" plugins[plugin_name.to_s.to_sym] end |
#load(loader, *args) ⇒ Object
Adds command(s) to the registry using the given loader
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/git_commander/registry.rb', line 49 def load(loader, *args) result = loader.new(self).load(*args) if result.success? result.plugins.each { |plugin| register_plugin(plugin) } result.commands.each { |cmd| register_command(cmd) } end result end |
#register(command_name, **options, &block) ⇒ Object
Adds a command to the registry
25 26 27 28 |
# File 'lib/git_commander/registry.rb', line 25 def register(command_name, **, &block) command = GitCommander::Command.new(command_name.to_sym, registry: self, **.merge(block: block)) register_command(command) end |
#register_command(command) ⇒ Object
Adds a pre-built command to the registry
32 33 34 35 36 |
# File 'lib/git_commander/registry.rb', line 32 def register_command(command) GitCommander.logger.debug "[#{logger_tag}] Registering command `#{command.name}` with args: #{command.inspect}..." commands[command.name] = command end |
#register_plugin(plugin) ⇒ Object
Adds a pre-built Plugin to the registry
40 41 42 43 44 |
# File 'lib/git_commander/registry.rb', line 40 def register_plugin(plugin) GitCommander.logger.debug "[#{logger_tag}] Registering plugin `#{plugin.name}`..." plugins[plugin.name] = plugin end |