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:



257
258
259
# File 'lib/dry/events/publisher.rb', line 257

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



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

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



189
190
191
192
# File 'lib/dry/events/publisher.rb', line 189

def publish(event_id, payload = EMPTY_HASH)
  __bus__.publish(event_id, payload)
  self
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)


179
180
181
182
# File 'lib/dry/events/publisher.rb', line 179

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



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/dry/events/publisher.rb', line 205

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)


235
236
237
# File 'lib/dry/events/publisher.rb', line 235

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

#unsubscribe(listener) ⇒ self

Unsubscribe a listener

Parameters:

  • listener (Object)

    The listener object

Returns:

  • (self)


228
229
230
# File 'lib/dry/events/publisher.rb', line 228

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