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/subscriber_registry.rb,
lib/spree/event/adapters/active_support_notifications.rb
Defined Under Namespace
Modules: Adapters, Subscriber Classes: Configuration, SubscriberRegistry
Instance Attribute Summary collapse
-
#subscribers ⇒ Spree::Event::SubscriberRegistry
readonly
The registry for supporting class reloading for Spree::Event::Subscriber instances.
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.
- #subscriber_registry ⇒ Object
-
#unsubscribe(subscriber) ⇒ Object
Unsubscribes a whole event or a specific subscription object.
Instance Attribute Details
#subscribers ⇒ Spree::Event::SubscriberRegistry (readonly)
Returns The registry for supporting class reloading for Spree::Event::Subscriber instances.
105 106 107 |
# File 'lib/spree/event.rb', line 105 def subscriber_registry Spree::Config.events.subscriber_registry end |
Instance Method Details
#adapter ⇒ Object
The adapter used by Spree::Event, defaults to Spree::Event::Adapters::ActiveSupportNotifications
99 100 101 |
# File 'lib/spree/event.rb', line 99 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.
26 27 28 29 30 |
# File 'lib/spree/event.rb', line 26 def fire(event_name, opts = {}) adapter.fire normalize_name(event_name), 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
88 89 90 |
# File 'lib/spree/event.rb', line 88 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.
55 56 57 58 59 |
# File 'lib/spree/event.rb', line 55 def subscribe(event_name, &block) name = normalize_name(event_name) listener_names << name adapter.subscribe(name, &block) end |
#subscriber_registry ⇒ Object
105 106 107 |
# File 'lib/spree/event.rb', line 105 def subscriber_registry Spree::Config.events.subscriber_registry end |
#unsubscribe(subscriber) ⇒ Object
Unsubscribes a whole event or a specific subscription object
73 74 75 76 |
# File 'lib/spree/event.rb', line 73 def unsubscribe(subscriber) name_or_subscriber = subscriber.is_a?(String) ? normalize_name(subscriber) : subscriber adapter.unsubscribe(name_or_subscriber) end |