Class: Marvin::CommandHandler
- Inherits:
-
Base
- Object
- Base
- Marvin::CommandHandler
show all
- Defined in:
- lib/marvin/command_handler.rb
Constant Summary
- @@exposed_method_mapping =
Hash.new { |h,k| h[k] = [] }
- @@method_descriptions =
Hash.new { |h,k| h[k] = {} }
Instance Attribute Summary
Attributes inherited from Base
#client, #from, #options, #target
Class Method Summary
(collapse)
Instance Method Summary
(collapse)
Methods inherited from Base
#_handle, #action, #addressed?, #ctcp, event_handlers_for, #from_channel?, #from_user?, #handle, #handle_incoming_numeric, #msg, on_event, on_numeric, #pm, register!, registered=, #registered=, registered?, reloaded!, #reply
Class Method Details
+ (Object) command(name, method_desc = nil, &blk)
14
15
16
17
18
|
# File 'lib/marvin/command_handler.rb', line 14
def command(name, method_desc = nil, &blk)
exposes name
desc method_desc unless method_desc.blank?
define_method(name, &blk)
end
|
+ (Object) desc(description)
42
43
44
|
# File 'lib/marvin/command_handler.rb', line 42
def desc(description)
@last_description = description
end
|
+ (Object) exposed_methods
28
29
30
31
32
33
34
35
36
|
# File 'lib/marvin/command_handler.rb', line 28
def exposed_methods
methods = []
klass = self
while klass != Object
methods += @@exposed_method_mapping[klass]
klass = klass.superclass
end
return methods.uniq.compact
end
|
+ (Object) exposed_name(method)
46
47
48
|
# File 'lib/marvin/command_handler.rb', line 46
def exposed_name(method)
"#{command_prefix}#{method}"
end
|
+ (Object) exposes(*args)
24
25
26
|
# File 'lib/marvin/command_handler.rb', line 24
def exposes(*args)
args.each { |name| @@exposed_method_mapping[self] << name.to_sym }
end
|
+ (Object) method_added(name)
88
89
90
91
92
93
|
# File 'lib/marvin/command_handler.rb', line 88
def self.method_added(name)
if @last_description.present?
@@method_descriptions[self][name.to_sym] = @last_description
@last_description = nil
end
end
|
+ (Object) prefix_is(p)
20
21
22
|
# File 'lib/marvin/command_handler.rb', line 20
def prefix_is(p)
self.command_prefix = p
end
|
+ (Object) prefix_regexp
38
39
40
|
# File 'lib/marvin/command_handler.rb', line 38
def prefix_regexp
/^#{command_prefix}/
end
|
+ (Object) reloading!
50
51
52
53
54
|
# File 'lib/marvin/command_handler.rb', line 50
def reloading!
super
@@exposed_method_mapping.delete(self)
@@method_descriptions.delete(self)
end
|
Instance Method Details
- (Object) check_for_commands
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/marvin/command_handler.rb', line 60
def check_for_commands
message = options.message.to_s.strip
data, command = nil, nil
if from_channel?
name, command, data = message.split(/\s+/, 2)
return if name !~ /^#{client.nickname}:/i
else
command, data = message.split(/\s+/, 2)
end
data ||= ""
if (command_name = extract_command_name(command)).present?
logger.info "Processing command '#{command_name}' for #{from}"
send(command_name, data.to_s) if respond_to?(command_name)
end
end
|
- (Object) exposed_name(name)
84
85
86
|
# File 'lib/marvin/command_handler.rb', line 84
def exposed_name(name)
self.class.exposed_name(name)
end
|
- (Object) extract_command_name(command)
76
77
78
79
80
81
82
|
# File 'lib/marvin/command_handler.rb', line 76
def extract_command_name(command)
re = self.class.prefix_regexp
if command =~ re
method_name = command.gsub(re, "").underscore.to_sym
return method_name if self.class.exposed_methods.include?(method_name)
end
end
|