Class: Zephira::Commands

Inherits:
Object
  • Object
show all
Defined in:
lib/zephira/commands.rb,
lib/zephira/commands/bye.rb,
lib/zephira/commands/help.rb,
lib/zephira/commands/about.rb,
lib/zephira/commands/clear.rb,
lib/zephira/commands/model.rb,
lib/zephira/commands/compact.rb,
lib/zephira/commands/history.rb

Defined Under Namespace

Classes: About, Bye, Clear, Compact, Help, History, Model

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.load(paths:) ⇒ Object



5
6
7
8
9
10
# File 'lib/zephira/commands.rb', line 5

def self.load(paths:)
  paths.each do |path|
    Dir.glob(File.join(path, "**", "*.rb")).each { |file| require file }
  end
  new
end

Instance Method Details

#constantsObject



12
13
14
15
16
# File 'lib/zephira/commands.rb', line 12

def constants
  @constants ||= ::Zephira::Commands.constants(false).map do |name|
    ::Zephira::Commands.const_get(name)
  end
end

#run(name:, args:, agent:) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/zephira/commands.rb', line 18

def run(name:, args:, agent:)
  command_class = constants.find { |command| command.name == name }
  if command_class.nil?
    puts "Unknown command '/#{name}'. Type /help for a list of commands."
  else
    command_class.run(agent: agent, args: args)
  end
end

#to_hObject



27
28
29
# File 'lib/zephira/commands.rb', line 27

def to_h
  constants.map { |command| {name: command.name, description: command.description} }
end