Module: Lita::Handler::EventRouter
- Included in:
- Lita::Handler, Lita::Handlers::DeprecationCheck
- Defined in:
- lib/lita/handler/event_router.rb
Overview
A handler mixin that provides the methods necessary for handling events.
Class Method Summary collapse
-
.extended(klass) ⇒ Object
Includes common handler methods in any class that includes EventRouter.
Instance Method Summary collapse
-
#event_subscriptions_for(event_name) ⇒ Array
Returns an array of all callbacks registered for the named event.
- #on(event_name, method_name_or_callable = nil, &block) ⇒ Object
-
#trigger(robot, event_name, payload = {}) ⇒ Boolean
Triggers an event, invoking methods previously registered with #on and passing them a payload hash with any arbitrary data.
Class Method Details
.extended(klass) ⇒ Object
Includes common handler methods in any class that includes Lita::Handler::EventRouter.
7 8 9 |
# File 'lib/lita/handler/event_router.rb', line 7 def self.extended(klass) klass.send(:include, Common) end |
Instance Method Details
#event_subscriptions_for(event_name) ⇒ Array
Returns an array of all callbacks registered for the named event.
48 49 50 |
# File 'lib/lita/handler/event_router.rb', line 48 def event_subscriptions_for(event_name) event_subscriptions[normalize_event(event_name)] end |
#on(event_name, method_name) ⇒ void #on(event_name, callable) ⇒ void #on(event_name) { ... } ⇒ void
38 39 40 41 42 |
# File 'lib/lita/handler/event_router.rb', line 38 def on(event_name, method_name_or_callable = nil, &block) event_subscriptions[normalize_event(event_name)] << Callback.new( method_name_or_callable || block ) end |
#trigger(robot, event_name, payload = {}) ⇒ Boolean
Triggers an event, invoking methods previously registered with #on and passing them a payload hash with any arbitrary data.
58 59 60 61 62 63 64 65 66 |
# File 'lib/lita/handler/event_router.rb', line 58 def trigger(robot, event_name, payload = {}) event_subscriptions_for(event_name).map do |callback| begin callback.call(new(robot), payload) rescue => error log_error(robot, error) end end.any? end |