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>)


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.

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



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

Parameters:

  • name (String)

Returns:

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

Parameters:



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

Visible commands for /help (excludes hidden commands).

Returns:



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