Module: Spree::Event
- Extended by:
- Event
- Included in:
- Event
- Defined in:
- lib/spree/event.rb,
lib/spree/event/subscriber.rb,
lib/spree/event/configuration.rb,
lib/spree/event/adapters/active_support_notifications.rb
Defined Under Namespace
Modules: Adapters, Subscriber Classes: Configuration
Instance Attribute Summary collapse
- #subscribers ⇒ Object readonly
Instance Method Summary collapse
-
#adapter ⇒ Object
The adapter used by Spree::Event, defaults to Spree::Event::Adapters::ActiveSupportNotifications.
-
#fire(event_name, opts = {}) ⇒ Object
Allows to trigger events that can be subscribed using #subscribe.
-
#listeners ⇒ Hash
Lists all subscriptions currently registered under the “.spree” namespace.
-
#subscribe(event_name, &block) ⇒ Object
Subscribe to an event with the given name.
-
#suffix ⇒ Object
The suffix used for namespacing Solidus events, defaults to ‘.spree`.
-
#unsubscribe(subscriber) ⇒ Object
Unsubscribes a whole event or a specific subscription object.
Instance Attribute Details
Instance Method Details
#adapter ⇒ Object
The adapter used by Spree::Event, defaults to Spree::Event::Adapters::ActiveSupportNotifications
89 90 91 |
# File 'lib/spree/event.rb', line 89 def adapter Spree::Config.events.adapter end |
#fire(event_name, opts = {}) ⇒ Object
Allows to trigger events that can be subscribed using #subscribe. An optional block can be passed that will be executed immediately. The actual code implementation is delegated to the adapter.
23 24 25 26 27 |
# File 'lib/spree/event.rb', line 23 def fire(event_name, opts = {}) adapter.fire name_with_suffix(event_name.to_s), opts do yield opts if block_given? end end |
#listeners ⇒ Hash
Lists all subscriptions currently registered under the “.spree” namespace. Actual implementation is delegated to the adapter
78 79 80 |
# File 'lib/spree/event.rb', line 78 def listeners adapter.listeners_for(listener_names) end |
#subscribe(event_name, &block) ⇒ Object
Subscribe to an event with the given name. The provided block is executed every time the subscribed event is fired.
45 46 47 48 49 |
# File 'lib/spree/event.rb', line 45 def subscribe(event_name, &block) name = name_with_suffix(event_name) listener_names << name adapter.subscribe(name, &block) end |
#suffix ⇒ Object
The suffix used for namespacing Solidus events, defaults to ‘.spree`
97 98 99 |
# File 'lib/spree/event.rb', line 97 def suffix Spree::Config.events.suffix end |
#unsubscribe(subscriber) ⇒ Object
Unsubscribes a whole event or a specific subscription object
63 64 65 66 |
# File 'lib/spree/event.rb', line 63 def unsubscribe(subscriber) name_or_subscriber = subscriber.is_a?(String) ? name_with_suffix(subscriber) : subscriber adapter.unsubscribe(name_or_subscriber) end |