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_class) ⇒ void
Register a command class.
-
#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).
43 44 45 |
# File 'lib/rubyn_code/cli/commands/registry.rb', line 43 def completions @commands.keys.sort.freeze end |
#dispatch(name, args, ctx) ⇒ Symbol?
Look up and execute a command by name.
33 34 35 36 37 38 |
# File 'lib/rubyn_code/cli/commands/registry.rb', line 33 def dispatch(name, args, ctx) command_class = @commands[name] return :unknown unless command_class command_class.new.execute(args, ctx) end |
#known?(name) ⇒ Boolean
58 59 60 |
# File 'lib/rubyn_code/cli/commands/registry.rb', line 58 def known?(name) @commands.key?(name) end |
#register(command_class) ⇒ void
This method returns an undefined value.
Register a command class.
20 21 22 23 24 25 |
# File 'lib/rubyn_code/cli/commands/registry.rb', line 20 def register(command_class) @classes << command_class command_class.all_names.each do |name| @commands[name] = command_class end end |
#visible_commands ⇒ Array<Class<Commands::Base>>
Visible commands for /help (excludes hidden commands).
50 51 52 53 54 |
# File 'lib/rubyn_code/cli/commands/registry.rb', line 50 def visible_commands @classes .reject(&:hidden?) .sort_by(&:command_name) end |