Class: Evey::Dispatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/evey/dispatcher.rb

Defined Under Namespace

Classes: ReactorSet, RuleSet

Class Method Summary collapse

Class Method Details

.configureObject



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

Returns:

  • (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

.rulesObject



45
46
47
# File 'lib/evey/dispatcher.rb', line 45

def self.rules
  @rules ||= RuleSet.new
end