class Dry::Events::Publisher
Extension used for classes that can publish events
@example
class AppEvents include Dry::Events::Publisher[:app] register_event('users.created') end class CreateUser attr_reader :events def initialize(events) @events = events end def call(user) # do your thing events.publish('users.created', user: user, time: Time.now) end end app_events = AppEvents.new create_user = CreateUser.new(app_events) # this will publish "users.created" event with its payload create_user.call(name: "Jane")
@api public
Attributes
id[R]
@!attribute [r] :id
@return [Symbol,String] the publisher identifier @api private
Public Class Methods
[](id)
click to toggle source
Create a publisher extension with the provided identifier
@param [Symbol,String] id The identifier
@return [Publisher]
@raise PublisherAlreadyRegisteredError
@api public
# File lib/dry/events/publisher.rb, line 104 def self.[](id) raise PublisherAlreadyRegisteredError.new(id) if registry.key?(id) new(id) end
new(id)
click to toggle source
@api private
# File lib/dry/events/publisher.rb, line 111 def initialize(id) @id = id end
registry()
click to toggle source
Internal publisher registry, which is used to identify them globally
This allows us to have listener classes that can subscribe to events without having access to instances of publishers yet.
@api private
# File lib/dry/events/publisher.rb, line 86 def self.registry @__registry__ ||= Concurrent::Map.new end
Public Instance Methods
included(klass)
click to toggle source
Hook for inclusions/extensions
It registers the publisher class under global registry using the id
@api private
Calls superclass method
# File lib/dry/events/publisher.rb, line 120 def included(klass) klass.extend(ClassMethods) klass.include(InstanceMethods) self.class.registry[id] = klass super end