Class: Botiasloop::Commands::Registry

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

Overview

Registry for slash commands Manages command registration, lookup, and execution

Constant Summary collapse

COMMAND_PATTERN =

Regex pattern to match slash commands at the start of a message Captures command name and optional arguments

/^\/([a-zA-Z0-9_]+)(?:\s+(.+))?$/

Instance Method Summary collapse

Constructor Details

#initializeRegistry

Returns a new instance of Registry.



12
13
14
# File 'lib/botiasloop/commands/registry.rb', line 12

def initialize
  @commands = {}
end

Instance Method Details

#[](name) ⇒ Class?

Get a command class by name

Parameters:

  • name (Symbol)

    Command name

Returns:

  • (Class, nil)

    Command class or nil if not found



31
32
33
# File 'lib/botiasloop/commands/registry.rb', line 31

def [](name)
  @commands[name]
end

#allArray<Class>

Get all registered command classes

Returns:

  • (Array<Class>)

    Array of command classes sorted by name



38
39
40
# File 'lib/botiasloop/commands/registry.rb', line 38

def all
  @commands.values.sort_by(&:command_name)
end

#clearObject

Clear all registered commands Useful for testing to prevent state leakage



51
52
53
# File 'lib/botiasloop/commands/registry.rb', line 51

def clear
  @commands.clear
end

#command?(message) ⇒ Boolean

Check if a message is a valid command Must start with / and be a registered command

Parameters:

  • message (String)

    Message text to check

Returns:

  • (Boolean)

    True if message is a registered command



60
61
62
63
64
65
66
67
68
# File 'lib/botiasloop/commands/registry.rb', line 60

def command?(message)
  return false unless message.is_a?(String)

  match = message.match(COMMAND_PATTERN)
  return false unless match

  name = match[1]&.to_sym
  @commands.key?(name)
end

#execute(message, context) ⇒ String

Execute a command from a message

Parameters:

  • message (String)

    Full command message (e.g., “/help” or “/switch label”)

  • context (Context)

    Execution context

Returns:

  • (String)

    Command response or error message



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/botiasloop/commands/registry.rb', line 75

def execute(message, context)
  match = message.match(COMMAND_PATTERN)
  return unknown_command_response(message) unless match

  name = match[1]&.to_sym
  args = match[2]

  command_class = @commands[name]
  return unknown_command_response(message) unless command_class

  command = command_class.new
  command.execute(context, args)
end

#namesArray<Symbol>

Get all registered command names

Returns:

  • (Array<Symbol>)

    Array of command names sorted



45
46
47
# File 'lib/botiasloop/commands/registry.rb', line 45

def names
  @commands.keys.sort
end

#register(command_class) ⇒ Object

Register a command class

Parameters:

  • command_class (Class)

    Command class inheriting from Base

Raises:

  • (Error)

    If command class doesn’t have a command_name



20
21
22
23
24
25
# File 'lib/botiasloop/commands/registry.rb', line 20

def register(command_class)
  name = command_class.command_name
  raise Error, "Command class must define command name" unless name

  @commands[name] = command_class
end