lib/marvin/command_handler.rb in Sutto-marvin-0.2.4 vs lib/marvin/command_handler.rb in Sutto-marvin-0.3.0

- old
+ new

@@ -1,21 +1,27 @@ +require 'set' + module Marvin # A Simple Marvin handler based on processing # commands, similar in design to MatzBot. class CommandHandler < Base class_inheritable_accessor :exposed_methods, :command_prefix + cattr_accessor :descriptions, :last_description, :exposed_method_names - self.command_prefix = "" - self.exposed_methods = [] + self.command_prefix = "" + self.exposed_methods = Set.new + self.descriptions = {} + self.exposed_method_names = Set.new class << self def exposes(*args) - self.exposed_methods ||= [] - self.exposed_methods += args.map { |a| a.to_sym }.flatten + names = args.map { |a| a.to_sym }.flatten + self.exposed_methods += names + self.exposed_method_names += names end end on_event :incoming_message do @@ -50,9 +56,24 @@ has_prefix = command[0...prefix_length] == self.command_prefix.to_s if has_prefix method_name = command[prefix_length..-1].to_s.underscore.to_sym return method_name if self.exposed_methods.to_a.include?(method_name) end + end + + class << self + + def desc(desc) + self.last_description = desc + end + + def method_added(name) + unless last_description.blank? + descriptions[name.to_sym] = self.last_description + self.last_description = nil + end + end + end end end \ No newline at end of file