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:



180
181
182
# File 'core/lib/rom/support/notifications.rb', line 180

def self.event_bus(id)
  EventBus.new(id, events: events.dup, listeners: listeners.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)


160
161
162
# File 'core/lib/rom/support/notifications.rb', line 160

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