Class: Botiasloop::Commands::Registry
- Inherits:
-
Object
- Object
- Botiasloop::Commands::Registry
- 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
-
#[](name) ⇒ Class?
Get a command class by name.
-
#all ⇒ Array<Class>
Get all registered command classes.
-
#clear ⇒ Object
Clear all registered commands Useful for testing to prevent state leakage.
-
#command?(message) ⇒ Boolean
Check if a message is a valid command Must start with / and be a registered command.
-
#execute(message, context) ⇒ String
Execute a command from a message.
-
#initialize ⇒ Registry
constructor
A new instance of Registry.
-
#names ⇒ Array<Symbol>
Get all registered command names.
-
#register(command_class) ⇒ Object
Register a command class.
Constructor Details
#initialize ⇒ Registry
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
31 32 33 |
# File 'lib/botiasloop/commands/registry.rb', line 31 def [](name) @commands[name] end |
#all ⇒ Array<Class>
Get all registered command classes
38 39 40 |
# File 'lib/botiasloop/commands/registry.rb', line 38 def all @commands.values.sort_by(&:command_name) end |
#clear ⇒ Object
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
60 61 62 63 64 65 66 67 68 |
# File 'lib/botiasloop/commands/registry.rb', line 60 def command?() return false unless .is_a?(String) match = .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
75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/botiasloop/commands/registry.rb', line 75 def execute(, context) match = .match(COMMAND_PATTERN) return unknown_command_response() unless match name = match[1]&.to_sym args = match[2] command_class = @commands[name] return unknown_command_response() unless command_class command = command_class.new command.execute(context, args) end |
#names ⇒ Array<Symbol>
Get all registered command names
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
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 |