Module: Dry::Events::Publisher::InstanceMethods

Defined in:
lib/dry/events/publisher.rb

Overview

Instance interface for publishers

Instance Method Summary collapse

Instance Method Details

#__bus__Bus

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.

Internal event bus

Returns:



277
278
279
# File 'lib/dry/events/publisher.rb', line 277

def __bus__
  @__bus__ ||= self.class.new_bus
end

#process(event_id, payload = EMPTY_HASH, &block) ⇒ Object

Utility method which yields event with each of its listeners

Listeners are already filtered out when filter was provided during subscription

param [Hash] payload An optional payload

Parameters:

  • event_id (Symbol, String)

    The event identifier



268
269
270
# File 'lib/dry/events/publisher.rb', line 268

def process(event_id, payload = EMPTY_HASH, &block)
  __bus__.process(event_id, payload, &block)
end

#publish(event_id, payload = EMPTY_HASH) ⇒ Object Also known as: trigger

Publish an event

Parameters:

  • event_id (String)

    The event identifier

  • payload (Hash) (defaults to: EMPTY_HASH)

    An optional payload



205
206
207
208
209
210
211
212
# File 'lib/dry/events/publisher.rb', line 205

def publish(event_id, payload = EMPTY_HASH)
  if __bus__.can_handle?(event_id)
    __bus__.publish(event_id, payload)
    self
  else
    raise UnregisteredEventError, event_id
  end
end

#register_event(event_id, payload = EMPTY_HASH) ⇒ self

Register a new event type at instance level

Parameters:

  • event_id (Symbol, String)

    The event identifier

  • payload (Hash) (defaults to: EMPTY_HASH)

    Optional default payload

Returns:

  • (self)


194
195
196
197
# File 'lib/dry/events/publisher.rb', line 194

def register_event(event_id, payload = EMPTY_HASH)
  __bus__.events[event_id] = Event.new(event_id, payload)
  self
end

#subscribe(object_or_event_id, filter_hash = EMPTY_HASH, &block) ⇒ Object

Subscribe to events.

If the filter parameter is provided, filters events by payload.

Parameters:

  • object_or_event_id (Symbol, String, Object)

    The event identifier or a listener object

  • filter_hash (Hash) (defaults to: EMPTY_HASH)

    An optional event filter

Returns:

  • (Object)

    self



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/dry/events/publisher.rb', line 225

def subscribe(object_or_event_id, filter_hash = EMPTY_HASH, &block)
  if __bus__.can_handle?(object_or_event_id)
    filter = Filter.new(filter_hash)

    if block
      __bus__.subscribe(object_or_event_id, filter, &block)
    else
      __bus__.attach(object_or_event_id, filter)
    end

    self
  else
    raise InvalidSubscriberError, object_or_event_id
  end
end

#subscribed?(listener) ⇒ Boolean

Return true if a given listener has been subscribed to any event

Returns:

  • (Boolean)


255
256
257
# File 'lib/dry/events/publisher.rb', line 255

def subscribed?(listener)
  __bus__.subscribed?(listener)
end

#unsubscribe(listener) ⇒ self

Unsubscribe a listener

Parameters:

  • listener (Object)

    The listener object

Returns:

  • (self)


248
249
250
# File 'lib/dry/events/publisher.rb', line 248

def unsubscribe(listener)
  __bus__.detach(listener)
end