Class: Jabot::Commands
- Inherits:
-
Object
show all
- Defined in:
- lib/jabot/commands.rb
Defined Under Namespace
Classes: CommandNotExists
Instance Method Summary
collapse
Constructor Details
Returns a new instance of Commands.
8
9
10
|
# File 'lib/jabot/commands.rb', line 8
def initialize
@commands_list = {}
end
|
Instance Method Details
#add_command(name, &block) ⇒ Object
12
13
14
|
# File 'lib/jabot/commands.rb', line 12
def add_command(name, &block)
@commands_list[name] = block unless command_exists? name
end
|
#available_commands ⇒ Object
41
42
43
44
45
46
47
48
|
# File 'lib/jabot/commands.rb', line 41
def available_commands
@commands_list.map do |key, value|
{
name: key.to_s,
args: value.parameters.map { |item| item[1] }.join(', ')
}
end
end
|
#command_exists?(command_name) ⇒ Boolean
16
17
18
|
# File 'lib/jabot/commands.rb', line 16
def command_exists?(command_name)
@commands_list.include? command_name
end
|
#parse(command_line) ⇒ Object
33
34
35
36
37
38
39
|
# File 'lib/jabot/commands.rb', line 33
def parse(command_line)
args = command_line.split
{
:name => args.shift.to_sym,
:args => args
}
end
|
#run(command_line) ⇒ Object
20
21
22
23
24
25
26
27
28
29
30
31
|
# File 'lib/jabot/commands.rb', line 20
def run(command_line)
c = parse command_line
unless command_exists? c[:name].downcase
raise CommandNotExists, "command '#{c[:name]}' not found"
end
begin
@commands_list[c[:name].downcase].call(*c[:args])
rescue
'Error while executing command'
end
end
|