Class: Marvin::CommandHandler

Inherits:
Base
  • Object
show all
Defined in:
lib/marvin/command_handler.rb

Direct Known Subclasses

CoreCommands, LoggingHandler

Constant Summary collapse

@@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

.command(name, method_desc = nil, &blk) ⇒ Object



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

.desc(description) ⇒ Object



42
43
44
# File 'lib/marvin/command_handler.rb', line 42

def desc(description)
  @last_description = description
end

.exposed_methodsObject



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

.exposed_name(method) ⇒ Object



46
47
48
# File 'lib/marvin/command_handler.rb', line 46

def exposed_name(method)
  "#{command_prefix}#{method}"
end

.exposes(*args) ⇒ Object



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

.method_added(name) ⇒ Object



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

.prefix_is(p) ⇒ Object



20
21
22
# File 'lib/marvin/command_handler.rb', line 20

def prefix_is(p)
  self.command_prefix = p
end

.prefix_regexpObject



38
39
40
# File 'lib/marvin/command_handler.rb', line 38

def prefix_regexp
  /^#{command_prefix}/
end

.reloading!Object



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

#check_for_commandsObject



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.splt(/\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

#exposed_name(name) ⇒ Object



84
85
86
# File 'lib/marvin/command_handler.rb', line 84

def exposed_name(name)
  self.class.exposed_name(name)
end

#extract_command_name(command) ⇒ Object



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