Class: Reactor::Event

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

Constant Summary collapse

CONSOLE_CONFIRMATION_MESSAGE =
<<-eos
  It looks like you are on a production console. Only fire an event if you intend to trigger 
  all of its subscribers. In order to proceed, you must pass `srsly: true` in the event data. To
  configure Reactor to ignore this validation, set `ENV['REACTOR_CONSOLE_ENABLED']` to a value.
eos

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ Event

Returns a new instance of Event.



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

def initialize(data = {})
  self.__data__ = {}.with_indifferent_access
  data.each do |key, value|
    value = value.encode('UTF-8', invalid: :replace, undef: :replace, replace: '') if value.is_a?(String)
    self.send("#{key}=", value)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



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

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__.



12
13
14
# File 'lib/reactor/event.rb', line 12

def __data__
  @__data__
end

Class Method Details

.perform(name, data) ⇒ Object



59
60
61
# File 'lib/reactor/event.rb', line 59

def perform(name, data)
  new.perform(name, data)
end

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



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/reactor/event.rb', line 63

def publish(name, data = {})
  if defined?(Rails::Console) && ENV['RACK_ENV'] == 'production' && data[:srsly].blank? &&
     !ENV['REACTOR_CONSOLE_ENABLED']
    raise ArgumentError.new(CONSOLE_CONFIRMATION_MESSAGE)
  end

  message = new(data.merge(event: name, uuid: SecureRandom.uuid))

  Reactor.validator.call(message)

  if message.at
    perform_at message.at, name, message.__data__
  else
    perform_async name, message.__data__
  end
end

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



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/reactor/event.rb', line 80

def reschedule(name, data = {})
  scheduled_jobs = Sidekiq::ScheduledSet.new
  # Note that scheduled_jobs#fetch returns only jobs matching the data[:was]
  # timestamp - down to fractions of a second
  job = scheduled_jobs.fetch(data[:was].to_f).detect do |job|
    next if job['class'] != self.name.to_s

    same_event_name  = job['args'].first == name.to_s

    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_actor
    else
      same_event_name
    end
  end

  job.delete if job

  publish(name, data.except([:was, :if])) if data[:at].try(:future?)
end

Instance Method Details

#perform(name, data) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/reactor/event.rb', line 22

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_block_subscribers(data, name)
  end
end

#to_sObject



54
55
56
# File 'lib/reactor/event.rb', line 54

def to_s
  name
end