Module: ROM::Notifications

Extended by:
Publisher
Included in:
Configuration
Defined in:
core/lib/rom/support/notifications.rb

Overview

Notification subsystem

This is an abstract event bus that implements a simple pub/sub protocol. The Notifications module is used in the setup process to decouple different modules from each other.

Examples:

class Setup
  extend ROM::Notifications

  register_event('setup.before_setup')
  register_event('setup.after_setup')

  def initialize
    @bus = Notifications.event_bus(:setup)
  end

  def setup
    @bus.trigger('setup.before_setup', at: Time.now)
    # ...
    @bus.trigger('setup.after_setup', at: Time.now)
  end
end

class Plugin
  extend ROM::Notifications::Listener

  subscribe('setup.after_setup') do |event|
    puts "Loaded at #{event.at.iso8601}"
  end
end

Defined Under Namespace

Modules: Listener, Publisher Classes: Event, EventBus

Constant Summary collapse

LISTENERS_HASH =
Hash.new { |h, k| h[k] = [] }

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.event_bus(id) ⇒ Notifications::EventBus

Build an event bus

Parameters:

  • id (Symbol)

    Bus key

Returns:



178
179
180
# File 'core/lib/rom/support/notifications.rb', line 178

def self.event_bus(id)
  EventBus.new(id, events: events.dup, listeners: listeners.dup)
end

.eventsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



163
164
165
# File 'core/lib/rom/support/notifications.rb', line 163

def self.events
  @__events__ ||= {}
end

.listenersObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



168
169
170
# File 'core/lib/rom/support/notifications.rb', line 168

def self.listeners
  @__listeners__ ||= LISTENERS_HASH.dup
end

.subscribe(event_id, query = EMPTY_HASH) {|block| ... } ⇒ Object Originally defined in module Publisher

Subscribe to events. If the query parameter is provided, filters events by payload.

Parameters:

  • event_id (String)

    The event key

  • query (Hash) (defaults to: EMPTY_HASH)

    An optional event filter

Yields:

  • (block)

    The callback

Returns:

  • (Object)

    self

.trigger(event_id, payload = EMPTY_HASH) ⇒ Object Originally defined in module Publisher

Trigger an event

Parameters:

  • event_id (String)

    The event key

  • payload (Hash) (defaults to: EMPTY_HASH)

    An optional payload

Instance Method Details

#register_event(id, info = EMPTY_HASH) ⇒ Object

Register an event

Parameters:

  • id (String)

    A unique event key

  • info (Hash) (defaults to: EMPTY_HASH)


158
159
160
# File 'core/lib/rom/support/notifications.rb', line 158

def register_event(id, info = EMPTY_HASH)
  Notifications.events[id] = Event.new(id, info)
end