Class: Dry::Events::Listener

Inherits:
Module
  • Object
show all
Defined in:
lib/dry/events/listener.rb

Overview

Extension for objects that can listen to events

Examples:

class AppEvents
  include Dry::Events::Publisher[:app]

  register_event("users.created")
end

class MyListener
  include Dry::Events::Listener[:app]

  subscribe("users.created") do |event|
    # do something
  end
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id) ⇒ Listener

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.

Returns a new instance of Listener.



43
44
45
46
47
48
49
50
# File 'lib/dry/events/listener.rb', line 43

def initialize(id)
  super()
  @id = id

  define_method(:subscribe) do |event_id, query = EMPTY_HASH, &block|
    Publisher.registry[id].subscribe(event_id, query, &block)
  end
end

Instance Attribute Details

#:id(: id) ⇒ Symbol, String (readonly)

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.

Returns The publisher identifier.

Returns:

  • (Symbol, String)

    The publisher identifier



31
# File 'lib/dry/events/listener.rb', line 31

attr_reader :id

#idObject (readonly)



31
32
33
# File 'lib/dry/events/listener.rb', line 31

def id
  @id
end

Class Method Details

.[](id) ⇒ Module

Create a listener extension for a specific publisher

Returns:

  • (Module)


38
39
40
# File 'lib/dry/events/listener.rb', line 38

def self.[](id)
  new(id)
end

Instance Method Details

#included(klass) ⇒ Object

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.



53
54
55
56
# File 'lib/dry/events/listener.rb', line 53

def included(klass)
  klass.extend(self)
  super
end