Class: Evey::Dispatcher
- Inherits:
-
Object
- Object
- Evey::Dispatcher
- Defined in:
- lib/evey/dispatcher.rb
Defined Under Namespace
Classes: ReactorSet, RuleSet
Class Method Summary collapse
- .configure ⇒ Object
- .disable! ⇒ Object
-
.dispatch(event) ⇒ Object
Dispatches events to matching Reactors once.
- .enable! ⇒ Object
- .enabled? ⇒ Boolean
-
.on(*events, sync: [], async: []) ⇒ Object
Register Reactors to Events.
- .rules ⇒ Object
Class Method Details
.configure ⇒ Object
14 15 16 17 18 19 |
# File 'lib/evey/dispatcher.rb', line 14 def self.configure if block_given? @rules = nil yield self end end |
.disable! ⇒ Object
6 7 8 |
# File 'lib/evey/dispatcher.rb', line 6 def self.disable! @disabled = true end |
.dispatch(event) ⇒ Object
Dispatches events to matching Reactors once. Called by all events after they are created.
37 38 39 40 41 42 43 |
# File 'lib/evey/dispatcher.rb', line 37 def self.dispatch(event) return unless enabled? reactors = rules.for(event) reactors.sync.each { |reactor| reactor.call(event) } reactors.async.each { |reactor| Evey::ReactorJob.perform_later(event, reactor.to_s) } end |
.enable! ⇒ Object
2 3 4 |
# File 'lib/evey/dispatcher.rb', line 2 def self.enable! @disabled = false end |
.enabled? ⇒ Boolean
10 11 12 |
# File 'lib/evey/dispatcher.rb', line 10 def self.enabled? @disabled != true end |
.on(*events, sync: [], async: []) ⇒ Object
Register Reactors to Events.
-
Reactors registered with ‘sync` will be triggered synchronously
-
Reactors registered with ‘async` will be triggered asynchronously via a Sidekiq Job
Example:
on BaseEvent, sync: LogEvent, async: TrackEvent
on PledgeCancelled, PaymentFailed, async: [NotifyAdmin, CreateTask]
on [PledgeCancelled, PaymentFailed], async: [NotifyAdmin, CreateTask]
31 32 33 |
# File 'lib/evey/dispatcher.rb', line 31 def self.on(*events, sync: [], async: []) rules.register(events: events.flatten, sync: Array(sync), async: Array(async)) end |