Class: RubynCode::CLI::Commands::Registry
- Inherits:
-
Object
- Object
- RubynCode::CLI::Commands::Registry
- Defined in:
- lib/rubyn_code/cli/commands/registry.rb
Overview
Discovers, registers, and dispatches slash commands.
Commands are registered by class reference. The registry builds a lookup table from command names + aliases → command class.
Instance Method Summary collapse
-
#completions ⇒ Array<String>
All registered command names (for tab completion).
-
#dispatch(name, args, ctx) ⇒ Symbol?
Look up and execute a command by name.
-
#initialize ⇒ Registry
constructor
A new instance of Registry.
- #known?(name) ⇒ Boolean
-
#register(command) ⇒ void
Register a command.
-
#visible_commands ⇒ Array<Class<Commands::Base>>
Visible commands for /help (excludes hidden commands).
Constructor Details
#initialize ⇒ Registry
Returns a new instance of Registry.
11 12 13 14 |
# File 'lib/rubyn_code/cli/commands/registry.rb', line 11 def initialize @commands = {} # '/name' => CommandClass @classes = [] end |
Instance Method Details
#completions ⇒ Array<String>
All registered command names (for tab completion).
48 49 50 |
# File 'lib/rubyn_code/cli/commands/registry.rb', line 48 def completions @commands.keys.sort.freeze end |
#dispatch(name, args, ctx) ⇒ Symbol?
Look up and execute a command by name.
35 36 37 38 39 40 41 42 43 |
# File 'lib/rubyn_code/cli/commands/registry.rb', line 35 def dispatch(name, args, ctx) command = @commands[name] return :unknown unless command # Built-ins are registered as classes (instantiate per call); # user-defined commands are registered as ready instances. instance = command.respond_to?(:new) ? command.new : command instance.execute(args, ctx) end |
#known?(name) ⇒ Boolean
63 64 65 |
# File 'lib/rubyn_code/cli/commands/registry.rb', line 63 def known?(name) @commands.key?(name) end |
#register(command) ⇒ void
This method returns an undefined value.
Register a command. Accepts either a command class (built-ins, which are instantiated per dispatch) or a ready command instance (user-defined; see Commands::CustomCommand).
22 23 24 25 26 27 |
# File 'lib/rubyn_code/cli/commands/registry.rb', line 22 def register(command) @classes << command command.all_names.each do |name| @commands[name] = command end end |
#visible_commands ⇒ Array<Class<Commands::Base>>
Visible commands for /help (excludes hidden commands).
55 56 57 58 59 |
# File 'lib/rubyn_code/cli/commands/registry.rb', line 55 def visible_commands @classes .reject(&:hidden?) .sort_by(&:command_name) end |