lib/reactor/models/subscriber.rb in reactor-0.2.2 vs lib/reactor/models/subscriber.rb in reactor-0.2.3
- old
+ new
@@ -10,14 +10,10 @@
self.message = Reactor::Event.new(data)
instance_exec &self.class.on_fire
self
end
- def delay_amount
- self.class.delay_amount
- end
-
class << self
def on_fire(&block)
if block
@fire_block = block
end
@@ -26,17 +22,20 @@
def fire(subscriber_id, data)
Reactor::Subscriber.find(subscriber_id).fire data
end
- def subscribes_to(name = nil, delay: nil)
- @delay_amount = delay
+ def subscribes_to(name = nil, data = {})
#subscribers << name
#TODO: REMEMBER SUBSCRIBERS so we can define them in code as well as with a row in the DB
+ # until then, here's a helper to make it easy to create with random data in postgres
+ # total crap I know but whatever
+ define_singleton_method :first_or_create! do
+ chain = where(event: name)
+ data.each do |key, value|
+ chain = chain.where("data @> '#{key}=#{value}'")
+ end
+ chain.first_or_create!(data)
+ end
end
-
- def delay_amount
- @delay_amount
- end
-
end
end
\ No newline at end of file