lib/uppercut/agent.rb in tyler-uppercut-0.7.0 vs lib/uppercut/agent.rb in tyler-uppercut-0.7.1

- old
+ new

@@ -14,14 +14,14 @@ # is a always an Uppercut::Message object, which can be used to reply # to the sender. The rest of the arguments to the block correspond to # any captures in the pattern Regexp. (Does not apply to String # patterns). def command(pattern,&block) - define_method(gensym) do |msg| - return :no_match unless captures = matches?(pattern,msg.body) - block[Conversation.new(msg.from,self),*captures] - end + @@patterns ||= [] + g = gensym + @@patterns << [pattern,g] + define_method(g, &block) end # Define a callback for specific presence events. # # At the moment this is only confirmed to work with :subscribe and :unsubscribe, but it may work with other types as well. @@ -37,11 +37,11 @@ end private def gensym - '__uc' + (self.instance_methods.grep(/^__uc/).size).to_s.rjust(8,'0') + ('__uc' + (self.instance_methods.grep(/^__uc/).size).to_s.rjust(8,'0')).intern end end DEFAULT_OPTIONS = { :connect => true } @@ -180,10 +180,15 @@ def dispatch(msg) bare_from = msg.from.bare block = @redirects[bare_from].respond_to?(:shift) && @redirects[bare_from].shift return block[msg.body] if block - self.methods.grep(/^__uc/).sort.detect { |m| send(m,msg) != :no_match } + captures = nil + pair = @@patterns.detect { |pattern,method| captures = matches?(pattern,msg.body) } + if pair + pattern, method = pair if pair + send method, Conversation.new(msg.from,self), captures + end end def dispatch_presence(type, presence) handler = "__on_#{type}" self.send(handler, Conversation.new(presence.from, self), presence) if respond_to?(handler)