Class: Spree::Subscriber
- Inherits:
-
Object
- Object
- Spree::Subscriber
- Defined in:
- app/models/spree/subscriber.rb
Overview
Base class for event subscribers.
Subscribers handle events published through the Spree event system. They provide a clean DSL for declaring which events to subscribe to and are automatically registered during Rails initialization.
Direct Known Subclasses
ExportSubscriber, InvitationEmailSubscriber, ProductMetricsSubscriber, ReportSubscriber
Class Method Summary collapse
-
.call(event) ⇒ Object
Class-level call method for when the class itself is used as subscriber.
-
.event_handlers ⇒ Hash<String, Symbol>
Get event handlers mapping.
-
.on(pattern, method_name) ⇒ void
DSL method to route specific events to specific methods.
-
.subscribes_to(*patterns, **options) ⇒ void
DSL method to declare which events this subscriber handles.
-
.subscription_options ⇒ Hash
Get subscription options.
-
.subscription_patterns ⇒ Array<String>
Get all subscription patterns for this subscriber.
Instance Method Summary collapse
-
#call(event) ⇒ void
Handle an event.
-
#handle(event) ⇒ Object
Default event handler.
Class Method Details
.call(event) ⇒ Object
Class-level call method for when the class itself is used as subscriber
137 138 139 |
# File 'app/models/spree/subscriber.rb', line 137 def call(event) new.call(event) end |
.event_handlers ⇒ Hash<String, Symbol>
Get event handlers mapping
130 131 132 |
# File 'app/models/spree/subscriber.rb', line 130 def event_handlers @event_handlers ||= {} end |
.on(pattern, method_name) ⇒ void
This method returns an undefined value.
DSL method to route specific events to specific methods
108 109 110 111 |
# File 'app/models/spree/subscriber.rb', line 108 def on(pattern, method_name) @event_handlers ||= {} @event_handlers[pattern.to_s] = method_name end |
.subscribes_to(*patterns, **options) ⇒ void
This method returns an undefined value.
DSL method to declare which events this subscriber handles
89 90 91 92 93 94 95 96 |
# File 'app/models/spree/subscriber.rb', line 89 def subscribes_to(*patterns, **) @subscription_patterns ||= [] = patterns.flatten.each do |pattern| @subscription_patterns << pattern.to_s end end |
.subscription_options ⇒ Hash
Get subscription options
123 124 125 |
# File 'app/models/spree/subscriber.rb', line 123 def ||= {} end |
.subscription_patterns ⇒ Array<String>
Get all subscription patterns for this subscriber
116 117 118 |
# File 'app/models/spree/subscriber.rb', line 116 def subscription_patterns @subscription_patterns ||= [] end |
Instance Method Details
#call(event) ⇒ void
This method returns an undefined value.
Handle an event
Override this method in subclasses to handle events. If you’ve defined event handlers with on, this method will route to the appropriate handler automatically.
150 151 152 153 154 155 156 157 158 159 |
# File 'app/models/spree/subscriber.rb', line 150 def call(event) handler = find_handler(event) if handler send(handler, event) else # Default behavior - subclasses should override handle(event) end end |
#handle(event) ⇒ Object
Default event handler
Override this in subclasses if not using the on DSL
166 167 168 |
# File 'app/models/spree/subscriber.rb', line 166 def handle(event) # Override in subclass end |