Class: StandardId::Events::Subscribers::Base
- Inherits:
-
Object
- Object
- StandardId::Events::Subscribers::Base
- Defined in:
- lib/standard_id/events/subscribers/base.rb
Overview
Base class for event subscribers
Direct Known Subclasses
AccountLockingSubscriber, AccountStatusSubscriber, LoggingSubscriber, PasswordResetDeliverySubscriber, PasswordlessDeliverySubscriber
Class Method Summary collapse
-
.attach ⇒ Array<Object>
Register this subscriber with the event system.
-
.attached? ⇒ Boolean
Check if the subscriber is currently attached.
-
.detach ⇒ void
Unregister this subscriber from the event system.
-
.subscribe_to(*event_names) ⇒ void
Subscribe to specific event(s).
-
.subscribe_to_pattern(pattern) ⇒ void
Subscribe to events matching a pattern.
-
.subscribed_events ⇒ Array<String>
Get the subscribed events.
-
.subscription_pattern ⇒ Regexp?
Get the subscription pattern.
Instance Method Summary collapse
-
#call(event) ⇒ Object
Process the event.
-
#handle(event) ⇒ void
Handle an event.
-
#handle_error(error, event) ⇒ Object
Handle errors during event processing.
Class Method Details
.attach ⇒ Array<Object>
Register this subscriber with the event system
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/standard_id/events/subscribers/base.rb', line 80 def attach instance = new @subscriptions ||= [] if subscription_pattern @subscriptions << Events.subscribe(subscription_pattern) do |event| instance.handle(event) end end subscribed_events.each do |event_name| @subscriptions << Events.subscribe(event_name) do |event| instance.handle(event) end end @subscriptions end |
.attached? ⇒ Boolean
Check if the subscriber is currently attached
116 117 118 |
# File 'lib/standard_id/events/subscribers/base.rb', line 116 def attached? @subscriptions&.any? end |
.detach ⇒ void
This method returns an undefined value.
Unregister this subscriber from the event system
103 104 105 106 107 108 109 110 |
# File 'lib/standard_id/events/subscribers/base.rb', line 103 def detach return unless @subscriptions @subscriptions.each do |subscription| Events.unsubscribe(subscription) end @subscriptions = [] end |
.subscribe_to(*event_names) ⇒ void
This method returns an undefined value.
Subscribe to specific event(s)
47 48 49 |
# File 'lib/standard_id/events/subscribers/base.rb', line 47 def subscribe_to(*event_names) @subscribed_events = event_names.flatten end |
.subscribe_to_pattern(pattern) ⇒ void
This method returns an undefined value.
Subscribe to events matching a pattern
56 57 58 |
# File 'lib/standard_id/events/subscribers/base.rb', line 56 def subscribe_to_pattern(pattern) @subscription_pattern = pattern end |
.subscribed_events ⇒ Array<String>
Get the subscribed events
64 65 66 |
# File 'lib/standard_id/events/subscribers/base.rb', line 64 def subscribed_events @subscribed_events || [] end |
.subscription_pattern ⇒ Regexp?
Get the subscription pattern
72 73 74 |
# File 'lib/standard_id/events/subscribers/base.rb', line 72 def subscription_pattern @subscription_pattern end |
Instance Method Details
#call(event) ⇒ Object
Process the event
Subclasses must implement this method.
143 144 145 |
# File 'lib/standard_id/events/subscribers/base.rb', line 143 def call(event) raise NotImplementedError, "#{self.class.name} must implement #call" end |
#handle(event) ⇒ void
This method returns an undefined value.
Handle an event
Override this method to add custom handling logic like
async processing or error handling. By default, it calls
the call method.
130 131 132 133 134 |
# File 'lib/standard_id/events/subscribers/base.rb', line 130 def handle(event) call(event) rescue StandardError => e handle_error(e, event) end |
#handle_error(error, event) ⇒ Object
Handle errors during event processing
Override this method to customize error handling. By default, it logs the error and re-raises.
156 157 158 159 160 161 |
# File 'lib/standard_id/events/subscribers/base.rb', line 156 def handle_error(error, event) StandardId.logger.error( "[StandardId::Events] Error in #{self.class.name} handling #{event.short_name}: #{error.}" ) raise error end |