Module: OnStomp::Interfaces::EventManager

Included in:
Failover::FailoverEvents, ClientEvents, ConnectionEvents
Defined in:
lib/onstomp/interfaces/event_manager.rb

Overview

Mixin for event management.

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

Extends base with ClassMethods



6
7
8
# File 'lib/onstomp/interfaces/event_manager.rb', line 6

def self.included(base)
  base.extend ClassMethods
end

Instance Method Details

#bind_event(event_name, cb_proc) ⇒ self

Binds a Proc to be invoked when the given event_name is triggered.

Parameters:

  • event_name (Symbol)
  • cb_proc (Proc)

Returns:

  • (self)


14
15
16
17
# File 'lib/onstomp/interfaces/event_manager.rb', line 14

def bind_event(event_name, cb_proc)
  event_callbacks[event_name] << cb_proc
  self
end

#event_callbacks{Symbol => Array<Proc>}

Returns a hash of event names mapped to arrays of proc callbacks.

Returns:

  • ({Symbol => Array<Proc>})


21
22
23
# File 'lib/onstomp/interfaces/event_manager.rb', line 21

def event_callbacks
  @event_callbacks ||= Hash.new { |h, k| h[k] = [] }
end

#trigger_event(event_name, *args) ⇒ Object

Triggers an event by the given name, passing along any additional args as parameters to the callback

Parameters:

  • event_name (Symbol)

    event to trigger

  • args (Object, Object, ...)


29
30
31
# File 'lib/onstomp/interfaces/event_manager.rb', line 29

def trigger_event(event_name, *args)
  event_callbacks[event_name].each { |cb| cb.call(*args) }
end