Class: Reactor::Event
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(data = {}) ⇒ Event
Returns a new instance of Event.
7
8
9
10
11
12
|
# File 'lib/reactor/event.rb', line 7
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
39
40
41
42
43
44
45
|
# File 'lib/reactor/event.rb', line 39
def method_missing(method, *args)
if method.to_s.include?('=')
try_setter(method, *args)
else
try_getter(method)
end
end
|
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
5
6
7
|
# File 'lib/reactor/event.rb', line 5
def data
@data
end
|
Class Method Details
52
53
54
|
# File 'lib/reactor/event.rb', line 52
def perform(name, data)
new.perform(name, data)
end
|
.publish(name, data = {}) ⇒ Object
56
57
58
59
60
61
62
63
64
|
# File 'lib/reactor/event.rb', line 56
def publish(name, data = {})
message = new(data.merge(event: name))
if message.at
perform_at message.at, name, message.data
else
perform_async name, message.data
end
end
|
.reschedule(name, data = {}) ⇒ Object
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
# File 'lib/reactor/event.rb', line 66
def reschedule(name, data = {})
scheduled_jobs = Sidekiq::ScheduledSet.new
job = scheduled_jobs.detect do |job|
next if job['class'] != self.name.to_s
same_event_name = job['args'].first == name.to_s
same_at_time = job.score.to_i == data[:was].to_i
if data[:actor]
same_actor = job['args'].second['actor_type'] == data[:actor].class.name &&
job['args'].second['actor_id'] == data[:actor].id
same_event_name && same_at_time && same_actor
else
same_event_name && same_at_time
end
end
job.delete if job
publish(name, data.except([:was, :if])) if data[:at].try(:future?)
end
|
Instance Method Details
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/reactor/event.rb', line 14
def perform(name, data)
data = data.with_indifferent_access
if data['actor_type']
actor = data["actor_type"].constantize.unscoped.find(data["actor_id"])
publishable_event = actor.class.events[name.to_sym]
ifarg = publishable_event[:if] if publishable_event
end
need_to_fire = case ifarg
when Proc
actor.instance_exec(&ifarg)
when Symbol
actor.send(ifarg)
when NilClass
true
end
if need_to_fire
data.merge!(fired_at: Time.current, name: name)
fire_database_driven_subscribers(data, name)
fire_block_subscribers(data, name)
end
end
|
#to_s ⇒ Object
47
48
49
|
# File 'lib/reactor/event.rb', line 47
def to_s
name
end
|