Class: Webcommand::Commands

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

Constant Summary collapse

CommandNotRegistered =
Class.new(StandardError)

Instance Method Summary collapse

Constructor Details

#initialize(commands_config) ⇒ Commands

Returns a new instance of Commands.



5
6
7
8
9
10
11
12
13
# File 'lib/webcommand/commands.rb', line 5

def initialize(commands_config)
  @commands = commands_config.map do |key, command_config|
    params_schema = command_config[:params]&.map { |key, value| [key, Regexp.new(value)] }.to_h

    [key, Command.new(command_config[:command], params_schema: params_schema)]
  end.to_h

  @commands.default_proc = ->(_,key) { raise CommandNotRegistered, "Command #{key} not found" }
end

Instance Method Details

#[](command_key) ⇒ Object



15
16
17
# File 'lib/webcommand/commands.rb', line 15

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