Class: EventEngine::Subscribers::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/event_engine/subscribers/base.rb

Overview

Base class for event subscribers. Subclasses declare the event they handle with subscribes_to and implement #handle(event). Declaring the subscription self-registers the subclass in the Registry at load time, so no explicit wiring is needed.

Examples:

class SendWelcomeEmail < EventEngine::Subscribers::Base
  subscribes_to :user_registered

  def handle(event)
    # ...
  end
end

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.subscribes_to(event_name) ⇒ void

This method returns an undefined value.

Registers this subscriber class for an event.

Parameters:

  • event_name (Symbol, String)

    the event to subscribe to



21
22
23
# File 'lib/event_engine/subscribers/base.rb', line 21

def self.subscribes_to(event_name)
  Registry.register(event_name, self)
end

Instance Method Details

#handle(event) ⇒ Object

Handles a dispatched event. Subclasses must override this.

Parameters:

  • event (Object)

    the dispatched event

Raises:

  • (NotImplementedError)

    if the subclass does not implement it



29
30
31
# File 'lib/event_engine/subscribers/base.rb', line 29

def handle(event)
  raise NotImplementedError, "#{self.class} must implement #handle"
end