Class: RubynCode::CLI::Commands::Registry

Inherits:
Object
  • Object
show all
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

Constructor Details

#initializeRegistry

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

#completionsArray<String>

All registered command names (for tab completion).

Returns:

  • (Array<String>)


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.

Parameters:

  • name (String)

    the slash command (e.g. ‘/doctor’)

  • args (Array<String>)

    arguments

  • ctx (Commands::Context)

    shared context

Returns:

  • (Symbol, nil)

    :quit if the command signals exit, nil otherwise



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

Parameters:

  • name (String)

Returns:

  • (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).

Parameters:



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_commandsArray<Class<Commands::Base>>

Visible commands for /help (excludes hidden commands).

Returns:



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