Class: Reactor::Event

Inherits:
Object
  • Object
show all
Includes:
OptionallySubclassable
Defined in:
lib/reactor/event.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ Event

Returns a new instance of Event.



6
7
8
9
10
11
# File 'lib/reactor/event.rb', line 6

def initialize(data = {})
  self.data = {}.with_indifferent_access
  data.each do |key, value|
    self.send("#{key}=", value)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/reactor/event.rb', line 13

def method_missing(method, *args)
  if method.to_s.include?('=')
    try_setter(method, *args)
  else
    try_getter(method)
  end
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



4
5
6
# File 'lib/reactor/event.rb', line 4

def data
  @data
end

Class Method Details

.process(name, data) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/reactor/event.rb', line 42

def self.process(name, data)
  # fire database listeners
  Reactor::Subscriber.where(event: name.to_s).each do |subscriber|
    Reactor::Subscriber.delay.fire subscriber.id, data
  end

  #TODO: support more matching?
  Reactor::Subscriber.where(event: '*').each do |s|
    Reactor::Subscriber.delay.fire s.id, data
  end
end

.publish(name, data = {}) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/reactor/event.rb', line 25

def self.publish(name, data = {})
  message = new(data.merge(event: name))
  if (message.at)
    delay_until(message.at).process name, message.data
  else
    delay.process name, message.data
  end
end

.reschedule(name, data = {}) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/reactor/event.rb', line 34

def self.reschedule(name, data = {})
  job = scheduled_jobs.detect do |job|
    job['class'] == name.to_s.camelize && job['at'].to_i == data[:was].to_i
  end
  remove_scheduled_job job if job
  delay.publish(name, data.except(:was)) if data[:at].future?
end

Instance Method Details

#to_sObject



21
22
23
# File 'lib/reactor/event.rb', line 21

def to_s
  name
end