lib/reactor/models/concerns/subscribable.rb in reactor-0.6.2 vs lib/reactor/models/concerns/subscribable.rb in reactor-0.7.0

- old
+ new

@@ -17,39 +17,41 @@ begin new_class = "#{handler_class_prefix}Handler#{i}" i+= 1 end while Reactor::StaticSubscribers.const_defined?(new_class) - eval %Q{ - class Reactor::StaticSubscribers::#{new_class} - include Sidekiq::Worker + klass = Class.new do + include Sidekiq::Worker - cattr_accessor :method, :delay, :source, :in_memory + class_attribute :method, :delay, :source, :in_memory, :dont_perform - def perform(data) - event = Reactor::Event.new(data) - if @@method.is_a?(Symbol) - @@source.delay_for(@@delay).send(@@method, event) - else - @@method.call(event) - end + def perform(data) + return :__perform_aborted__ if dont_perform && !Reactor::TEST_MODE_SUBSCRIBERS.include?(source) + event = Reactor::Event.new(data) + if method.is_a?(Symbol) + source.delay_for(delay).send(method, event) + else + method.call(event) end + end - def self.perform_where_needed(data) - if @@in_memory - new.perform(data) - else - perform_async(data) - end + def self.perform_where_needed(data) + if in_memory + new.perform(data) + else + perform_async(data) end end - } + end - new_class = "Reactor::StaticSubscribers::#{new_class}".constantize - new_class.method = method || block - new_class.delay = options[:delay] || 0 - new_class.source = options[:source] - new_class.in_memory = options[:in_memory] - new_class + Reactor::StaticSubscribers.const_set(new_class, klass) + + klass.tap do |k| + k.method = method || block + k.delay = options[:delay] || 0 + k.source = options[:source] + k.in_memory = options[:in_memory] + k.dont_perform = Reactor.test_mode? + end end end -end \ No newline at end of file +end