Class: Akasha::EventRouterBase

Inherits:
Object
  • Object
show all
Defined in:
lib/akasha/event_router_base.rb

Overview

Base class for routing events to event listeners.

Direct Known Subclasses

AsyncEventRouter, EventRouter

Instance Method Summary collapse

Constructor Details

#initialize(routes = {}) ⇒ EventRouterBase

Returns a new instance of EventRouterBase


4
5
6
7
# File 'lib/akasha/event_router_base.rb', line 4

def initialize(routes = {})
  @routes = Hash.new { |hash, key| hash[key] = [] }
  @routes.merge!(routes)
end

Instance Method Details

#register_event_listener(event_name, listener) ⇒ Object

Registers a new event listener, derived from `Akasha::EventListener`.


11
12
13
# File 'lib/akasha/event_router_base.rb', line 11

def register_event_listener(event_name, listener)
  @routes[event_name] << listener
end

#route(event_name, aggregate_id, **data) ⇒ Object

Routes an event.


16
17
18
19
20
21
22
23
24
25
# File 'lib/akasha/event_router_base.rb', line 16

def route(event_name, aggregate_id, **data)
  @routes[event_name].each do |listener|
    listener = listener.new if listener.is_a?(Class)
    begin
      listener.public_send(:"on_#{event_name}", aggregate_id, **data)
    rescue RuntimeError => e
      log "Error handling event #{event_name.inspect}: #{e}"
    end
  end
end