Class: GitCommander::Registry Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/git_commander/registry.rb

Overview

This class is abstract.

Manages available GitCommander commands

Defined Under Namespace

Classes: CommandNotFound, LoadError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRegistry

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

#commandsObject

Returns the value of attribute commands.



14
15
16
# File 'lib/git_commander/registry.rb', line 14

def commands
  @commands
end

#nameObject

Returns the value of attribute name.



14
15
16
# File 'lib/git_commander/registry.rb', line 14

def name
  @name
end

#pluginsObject

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

Examples:

Fetch a command from the registry

registry = GitCommander::Registry.new
registry.register :wtf
registry.find :wtf

Parameters:

  • command_name (String, Symbol)

    the name of the command to look up in the registry

Returns:

Raises:



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

Parameters:

  • loader (CommandLoader)

    the class to use to load with



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

Parameters:

  • command_name (String, Symbol)

    the name of the command to add to the registry



25
26
27
28
# File 'lib/git_commander/registry.rb', line 25

def register(command_name, **options, &block)
  command = GitCommander::Command.new(command_name.to_sym, registry: self, **options.merge(block: block))
  register_command(command)
end

#register_command(command) ⇒ Object

Adds a pre-built command to the registry

Parameters:

  • command (Command)

    the Command instance to add 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

Parameters:

  • plugin (Plugin)

    the Plugin instance to add 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