Class: Tasker::Events::Subscribers::BaseSubscriber
- Inherits:
-
Object
- Object
- Tasker::Events::Subscribers::BaseSubscriber
- Defined in:
- lib/tasker/events/subscribers/base_subscriber.rb
Overview
BaseSubscriber provides a clean foundation for creating custom event subscribers
This class extracts common patterns from TelemetrySubscriber and provides:
-
Declarative subscription registration via class methods
-
Automatic method routing from event names to handler methods
-
Defensive payload handling with safe accessors
-
Easy integration with the Tasker event system
-
Metrics collection helper methods for common patterns
Usage:
class OrderNotificationSubscriber < Tasker::Events::Subscribers::BaseSubscriber
subscribe_to :task_completed, :step_failed
def handle_task_completed(event)
OrderMailer.completion_email(event[:task_id]).deliver_later
end
def handle_step_failed(event)
AlertService.notify("Step failed: #{event[:step_name]}")
end
end
# Register the subscriber
OrderNotificationSubscriber.subscribe(Tasker::Events::Publisher.instance)
Direct Known Subclasses
Defined Under Namespace
Classes: ErrorCategorizer, MetricTagsExtractor
Class Method Summary collapse
-
.filter_events(&filter_proc) ⇒ void
Set a filter for events (optional).
-
.subscribe(publisher) ⇒ BaseSubscriber
Subscribe this subscriber to a publisher.
-
.subscribe_to(*events) ⇒ void
Declarative method to register events this subscriber cares about.
Instance Method Summary collapse
-
#initialize(name: nil, events: nil, config: {}) ⇒ BaseSubscriber
constructor
A new instance of BaseSubscriber.
-
#subscribe_to_publisher(publisher) ⇒ void
Subscribe to all events defined by the class.
Constructor Details
#initialize(name: nil, events: nil, config: {}) ⇒ BaseSubscriber
Returns a new instance of BaseSubscriber.
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/tasker/events/subscribers/base_subscriber.rb', line 34 def initialize(name: nil, events: nil, config: {}) @subscription_name = name @subscription_config = config # If events are provided via constructor (from YAML), add them to subscribed events return if events.blank? current_events = self.class.subscribed_events || [] self.class.subscribed_events = (current_events + Array(events)).uniq end |
Class Method Details
.filter_events(&filter_proc) ⇒ void
This method returns an undefined value.
Set a filter for events (optional)
Example:
filter_events { |event_name| event_name.include?('order') }
67 68 69 |
# File 'lib/tasker/events/subscribers/base_subscriber.rb', line 67 def filter_events(&filter_proc) self.event_filter = filter_proc end |
.subscribe(publisher) ⇒ BaseSubscriber
Subscribe this subscriber to a publisher
75 76 77 78 79 |
# File 'lib/tasker/events/subscribers/base_subscriber.rb', line 75 def subscribe(publisher) subscriber = new subscriber.subscribe_to_publisher(publisher) subscriber end |
.subscribe_to(*events) ⇒ void
This method returns an undefined value.
Declarative method to register events this subscriber cares about
Example:
subscribe_to :task_completed, :step_failed
subscribe_to 'order.created', 'payment.processed'
54 55 56 57 58 |
# File 'lib/tasker/events/subscribers/base_subscriber.rb', line 54 def subscribe_to(*events) # Accumulate events instead of replacing them current_events = subscribed_events || [] self.subscribed_events = (current_events + events.map(&:to_s)).uniq end |
Instance Method Details
#subscribe_to_publisher(publisher) ⇒ void
This method returns an undefined value.
Subscribe to all events defined by the class
86 87 88 89 90 91 92 93 94 95 |
# File 'lib/tasker/events/subscribers/base_subscriber.rb', line 86 def subscribe_to_publisher(publisher) event_subscriptions.each do |event_constant, handler_method| # Apply filtering if defined next unless should_handle_event?(event_constant) # Subscribe to the event with automatic method routing # This will fail fast if the event doesn't exist, which is the correct behavior publisher.subscribe(event_constant, &method(handler_method)) end end |