| Module | MUES::Event::Handler |
| In: |
lib/mues/events.rb
(CVS)
|
A default event handler mixin module. Including this module adds methods to the including class/module that are useful for an object which wishes to handle one or more event types.
Event dispatcher method. This method does dynamic dispatch to class-specific event handler methods in the class that mixes it in. It will look for a method called handle<eventClass>(), where eventClass is the class of the event to handle. If no explicit handler is found, each of the event‘s superclasses is tried as well. If no handler is defined for any of the events, it tries to call handleAnyEvent(). If no handler is found, a MUES::UnhandledEventError is raised.
# File lib/mues/events.rb, line 83 def handleEvent( event ) raise TypeError, "argument (#{event.to_s}) is not an event" unless event.is_a?( Event ) debugMsg( 1, "Handling a #{event.class.name} event." ) methodName = '' # Search the event's class heirarchy for Event subclasses, and # look up handler methods based on the class name event.class.ancestors.find_all {|klass| klass < Event }.each {|klass| eventType = klass.name.sub( /MUES::/, '' ) debugMsg( 2, "Checking for a handle#{eventType} method..." ) methodName = 'handle%s' % eventType if self.respond_to?( methodName ) debugMsg( 2, " found #{methodName}." ) return send( methodName, event ) end } ### Now call the default handler if it defines one debugMsg( 1, "Unable to handle the #{event.class.name}. Invoking the handleEvent method." ) return self.handleAnyEvent( event ) if self.respond_to?( :handleAnyEvent ) raise UnhandledEventError, "No handler defined for #{event.class.name}s" end
Register handlerObject to receive events of the specified eventClasses or any of their derivatives. See the docs for MUES::Event for how to handle events.
# File lib/mues/events.rb, line 116 def registerHandlerForEvents( handlerObject, *eventClasses ) checkResponse( handlerObject, "handleEvent" ) eventClasses.each do |eventClass| eventClass.registerHandlers( handlerObject ) end end
Unregister handlerObject as a handler for the specified eventClasses, or all event classes if no classes are specified.
# File lib/mues/events.rb, line 128 def unregisterHandlerForEvents( handlerObject, *eventClasses ) eventClasses = MUES::Event::getEventClasses if eventClasses.empty? eventClasses.each {|eventClass| eventClass.unregisterHandlers( handlerObject ) } end