Module: AI::Commands

Defined in:
lib/commands.rb

Overview

── Slash commands ───────────────────────────────────────────────────

Defined Under Namespace

Classes: Command

Constant Summary collapse

REGISTRY =
{}

Class Method Summary collapse

Class Method Details

.available_names(app) ⇒ Object



19
20
21
# File 'lib/commands.rb', line 19

def self.available_names(app)
  REGISTRY.values.select { |c| c.available?(app) }.map(&:name).sort
end

.complete_name(app, prefix) ⇒ Object



23
24
25
# File 'lib/commands.rb', line 23

def self.complete_name(app, prefix)
  available_names(app).select { |n| n.start_with?(prefix) }
end

.dispatch(app, input) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/commands.rb', line 27

def self.dispatch(app, input)
  name, rest = input.strip.split(/\s+/, 2)
  cmd = REGISTRY[name] or return :unknown
  return :unavailable unless cmd.available?(app)
  cmd.run.call(app, rest || "")
  :ok
end

.find(name) ⇒ Object



16
# File 'lib/commands.rb', line 16

def self.find(name)    = REGISTRY[name]

.register(name, desc:, available: nil, arg_complete: nil, &run) ⇒ Object



10
11
12
13
14
# File 'lib/commands.rb', line 10

def self.register(name, desc:, available: nil, arg_complete: nil, &run)
  REGISTRY[name] = Command.new(
    name: name, desc: desc, available: available, arg_complete: arg_complete, run: run
  )
end

.slash?(input) ⇒ Boolean

Returns:

  • (Boolean)


17
# File 'lib/commands.rb', line 17

def self.slash?(input) = input.start_with?("/")