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.

Since:

  • 4.0.0

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(klass) ⇒ Object

Includes common handler methods in any class that includes Lita::Handler::EventRouter.

Since:

  • 4.0.0


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.

Parameters:

  • event_name (String, Symbol)

    The name of the event to return callbacks for.

Returns:

  • (Array)

    The array of callbacks.

Since:

  • 4.0.0


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

Overloads:

  • #on(event_name, method_name) ⇒ void

    This method returns an undefined value.

    Registers an event subscription. When an event is triggered with #trigger, a new instance of the handler will be created and the instance method name supplied to #on will be invoked with a payload (a hash of arbitrary keys and values).

    Parameters:

    • event_name (String, Symbol)

      The name of the event to subscribe to.

    • method_name (String, Symbol)

      The name of the instance method on the handler that should be invoked when the event is triggered.

  • #on(event_name, callable) ⇒ void

    This method returns an undefined value.

    Registers an event subscription. When an event is triggered with #trigger, a new instance of the handler will be created and the callable object supplied to #on will be evaluated within the context of the new handler instance, and passed a payload (a hash of arbitrary keys and values).

    Parameters:

    • event_name (String, Symbol)

      The name of the event to subscribe to.

    • callable (#call)

      A callable object to serve as the event callback.

    Since:

    • 4.0.0

  • #on(event_name) { ... } ⇒ void

    This method returns an undefined value.

    Registers an event subscription. When an event is triggered with #trigger, a new instance of the handler will be created and the block supplied to #on will be evaluated within the context of the new handler instance, and passed a payload (a hash of arbitrary keys and values).

    Parameters:

    • event_name (String, Symbol)

      The name of the event to subscribe to.

    Yields:

    • The body of the event callback.

    Since:

    • 4.0.0

Since:

  • 4.0.0


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.

Parameters:

  • robot (Lita::Robot)

    The currently running robot instance.

  • event_name (String, Symbol)

    , The name of the event to trigger.

  • payload (Hash) (defaults to: {})

    An optional hash of arbitrary data.

Returns:

  • (Boolean)

    Whether or not the event triggered any callbacks.

Since:

  • 4.0.0


58
59
60
61
62
# File 'lib/lita/handler/event_router.rb', line 58

def trigger(robot, event_name, payload = {})
  event_subscriptions_for(event_name).map do |callback|
    callback.call(new(robot), payload)
  end.any?
end