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.



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

def initialize(id)
  @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



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

attr_reader :id

#idObject (readonly)



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

def id
  @id
end

Class Method Details

.[](id) ⇒ Module

Create a listener extension for a specific publisher

Returns:

  • (Module)


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

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.



51
52
53
54
# File 'lib/dry/events/listener.rb', line 51

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