Module: Operate::Pubsub::Publisher

Included in:
Command
Defined in:
lib/operate/pubsub/publisher.rb

Overview

A Command uses Publisher to register event handlers and broadcast to them.

Instance Method Summary collapse

Instance Method Details

#broadcast(event, *args) ⇒ self

Broadcasts an event

Examples:

def call
  # ...
  broadcast(:finished)
end

Returns:

  • (self)


33
34
35
36
37
38
# File 'lib/operate/pubsub/publisher.rb', line 33

def broadcast(event, *args)
  registrations.each do |registration|
    registration.broadcast(clean_event(event), *args)
  end
  self
end

#on(*events, &block) ⇒ self

Subscribe a block

Examples:

my_publisher.on(:order_created) { |args| ... }

Returns:

  • (self)

Raises:

  • (ArgumentError)


17
18
19
20
21
22
# File 'lib/operate/pubsub/publisher.rb', line 17

def on(*events, &block)
  raise ArgumentError, 'must give at least one event' if events.empty?
  raise ArgumentError, 'must pass a block' unless block
  registrations << Operate::Pubsub::Registration.new(block, on: events)
  self
end