reactor 0.3.2 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0f1aac22b0919076019fe2d53dd7999309d25216
4
- data.tar.gz: 0ea239c9e18da1b510ff0ef87971abc991425b9b
3
+ metadata.gz: 973132a3d45a07bd6de4a4563d543ac7531863a1
4
+ data.tar.gz: c25c41eacaa7e20a94e36c075218091ef27718e8
5
5
  SHA512:
6
- metadata.gz: 2ef2dabe5ccedc03b97990c583a992c6145716cd775d90fada6540a4b9395195e677db3e135d159240830be6701871a5615cbe966f769e3a4568b936bf925168
7
- data.tar.gz: 81f0d2605e65e50f3df59b2fe79f29e6ff327e8c658e6d9d6c6ee3e39bed8a0fbdb775551a33b7e888e68cd5efc21e6c44a32943eba30acf593bafc120a83d8a
6
+ metadata.gz: 2ac882931bf8e8d07851abf37bfd6c8847b2d3472759a9c733d7548f84fbdb759a867de17c73532550ec9238d6daca4c75e90946c95b38992c8d9db56d387ab5
7
+ data.tar.gz: 2e5c28ea31c91e8c9ce3fce8f41403a3e6ea59b526b00d516deb1ddff0ae2432f7ff0467ce876bf51d185049622cb1c4cdb5c3be068aeb6713836a2b3c2fdd89
data/lib/reactor/event.rb CHANGED
@@ -22,65 +22,63 @@ class Reactor::Event
22
22
  name
23
23
  end
24
24
 
25
- def self.publish(name, data = {})
26
- message = new(data.merge(event: name))
27
- #if (message.at)
28
- # delay_until(message.at).process name, message.data
29
- #else
30
- # delay.process name, message.data
31
- #end
32
-
33
- if message.at.nil?
34
- delay.process name, message.data
35
- elsif message.at.future?
36
- delay_until(message.at).process name, message.data
25
+ class << self
26
+ def publish(name, data = {})
27
+ message = new(data.merge(event: name))
28
+
29
+ if message.at.nil?
30
+ delay.process name, message.data
31
+ elsif message.at.future?
32
+ delay_until(message.at).process name, message.data
33
+ end
37
34
  end
38
- end
39
35
 
40
- def self.reschedule(name, data = {})
41
- job = scheduled_jobs.detect do |job|
42
- job['class'] == name.to_s.camelize && job['at'].to_i == data[:was].to_i
36
+ def reschedule(name, data = {})
37
+ job = scheduled_jobs.detect do |job|
38
+ job['class'] == name.to_s.camelize && job['at'].to_i == data[:was].to_i
39
+ end
40
+ remove_scheduled_job job if job
41
+ delay.publish(name, data.except(:was)) if data[:at].future?
43
42
  end
44
- remove_scheduled_job job if job
45
- delay.publish(name, data.except(:was)) if data[:at].future?
46
- end
47
43
 
48
- def self.process(name, data)
49
- # fire database listeners
50
- Reactor::Subscriber.where(event: name.to_s).each do |subscriber|
51
- Reactor::Subscriber.delay.fire subscriber.id, data
52
- end
44
+ def process(name, data)
45
+ data.merge!(fired_at: Time.current)
46
+ Reactor::Subscriber.where(event: name.to_s).each do |subscriber|
47
+ Reactor::Subscriber.delay.fire subscriber.id, data
48
+ end
53
49
 
54
- #TODO: support more matching?
55
- Reactor::Subscriber.where(event: '*').each do |s|
56
- Reactor::Subscriber.delay.fire s.id, data
57
- end
50
+ #TODO: support more matching?
51
+ Reactor::Subscriber.where(event: '*').each do |s|
52
+ Reactor::Subscriber.delay.fire s.id, data
53
+ end
58
54
 
59
- if (static_subscribers = Reactor::SUBSCRIBERS[name.to_s] || []).any?
60
- static_subscribers.each do |callback|
61
- case callback
62
- when Hash
63
- callback.keys.first.send callback.values.first, Reactor::Event.new(data.merge(event: name.to_s))
64
- else
65
- callback.call Reactor::Event.new(data.merge(event: name.to_s))
55
+ if (static_subscribers = Reactor::SUBSCRIBERS[name.to_s] || []).any?
56
+ static_subscribers.each do |callback|
57
+ delay = callback[:options].try(:[], :delay) || 0
58
+ case method = callback[:method]
59
+ when Symbol
60
+ callback[:source].delay_for(delay).send method, Reactor::Event.new(data.merge(event: name.to_s))
61
+ else
62
+ method.call Reactor::Event.new(data.merge(event: name.to_s))
63
+ end
66
64
  end
67
65
  end
68
66
  end
69
- end
70
67
 
71
- private
68
+ def scheduled_jobs(options = {})
69
+ Sidekiq.redis do |r|
70
+ from = options[:from] ? options[:from].to_f.to_s : '-inf'
71
+ to = options[:to] ? options[:to].to_f.to_s : '+inf'
72
+ r.zrangebyscore('schedule', from, to).map{|job| MultiJson.decode(job)}
73
+ end
74
+ end
72
75
 
73
- def self.scheduled_jobs(options = {})
74
- Sidekiq.redis do |r|
75
- from = options[:from] ? options[:from].to_f.to_s : '-inf'
76
- to = options[:to] ? options[:to].to_f.to_s : '+inf'
77
- r.zrangebyscore('schedule', from, to).map{|job| MultiJson.decode(job)}
76
+ def remove_scheduled_job(job)
77
+ Sidekiq.redis { |r| r.zrem 'schedule', MultiJson.encode(job) }
78
78
  end
79
79
  end
80
80
 
81
- def self.remove_scheduled_job(job)
82
- Sidekiq.redis { |r| r.zrem 'schedule', MultiJson.encode(job) }
83
- end
81
+ private
84
82
 
85
83
  def try_setter(method, object, *args)
86
84
  if object.is_a? ActiveRecord::Base
@@ -2,14 +2,9 @@ module Reactor::Subscribable
2
2
  extend ActiveSupport::Concern
3
3
 
4
4
  module ClassMethods
5
- def on_event(event, method = nil, &block)
6
- callback = case method
7
- when Symbol
8
- {self => method}
9
- else
10
- method
11
- end
12
- callback = block if block
5
+ def on_event(event, method = nil, options = {}, &block)
6
+ callback = {method: (method || block), options: {delay: 0}.merge(options)}
7
+ callback.merge!(source: self) if method.is_a? Symbol
13
8
  (Reactor::SUBSCRIBERS[event.to_s] ||= []).push(callback)
14
9
  end
15
10
  end
@@ -1,3 +1,3 @@
1
1
  module Reactor
2
- VERSION = "0.3.2"
2
+ VERSION = "0.4.0"
3
3
  end
data/spec/event_spec.rb CHANGED
@@ -24,9 +24,15 @@ describe Reactor::Event do
24
24
  end
25
25
 
26
26
  describe 'process' do
27
+ before { Reactor::Subscriber.create(event: :user_did_this) }
28
+ after { Reactor::Subscriber.destroy_all }
27
29
  it 'fires all subscribers' do
28
- Reactor::Subscriber.create(event: :user_did_this)
29
- Reactor::Subscriber.any_instance.should_receive(:fire).with(actor_id: '1')
30
+ Reactor::Subscriber.any_instance.should_receive(:fire).with(hash_including(actor_id: '1'))
31
+ Reactor::Event.process(event_name, actor_id: '1')
32
+ end
33
+
34
+ it 'sets a fired_at key in event data' do
35
+ Reactor::Subscriber.any_instance.should_receive(:fire).with(hash_including(fired_at: anything))
30
36
  Reactor::Event.process(event_name, actor_id: '1')
31
37
  end
32
38
  end
@@ -8,6 +8,7 @@ class Auction < ActiveRecord::Base
8
8
 
9
9
  on_event :puppy_delivered, :ring_bell
10
10
  on_event :any_event, -> (event) { puppies! }
11
+ on_event :pooped, :pick_up_poop, delay: 5.minutes
11
12
 
12
13
  def self.ring_bell(event)
13
14
  pp "ring ring! #{event}"
@@ -22,9 +23,18 @@ describe Reactor::Subscribable do
22
23
  Reactor::Event.publish(:bid_made, target: Auction.create)
23
24
  end
24
25
 
25
- it 'binds symbol to class method' do
26
- Auction.should_receive(:ring_bell)
27
- Reactor::Event.publish(:puppy_delivered)
26
+ describe 'binding symbol of class method' do
27
+ it 'fires on event' do
28
+ Auction.should_receive(:ring_bell)
29
+ Reactor::Event.publish(:puppy_delivered)
30
+ end
31
+
32
+ it 'can be delayed', :sidekiq do
33
+ Reactor::Event.process(:pooped, {})
34
+ job = Reactor::Event.scheduled_jobs(from: 4.minutes.from_now, to: 6.minutes.from_now).last
35
+ job.should be_present
36
+ job['args'].last.should include("pick_up_poop")
37
+ end
28
38
  end
29
39
 
30
40
  it 'binds proc' do
@@ -27,7 +27,7 @@ describe Reactor::Subscriber do
27
27
  describe 'matcher' do
28
28
  it 'can be set to star to bind to all events' do
29
29
  MySubscriber.create!(event: '*')
30
- MySubscriber.any_instance.should_receive(:fire).with({'random' => 'data', 'event' => :this_event})
30
+ MySubscriber.any_instance.should_receive(:fire).with(hash_including('random' => 'data', 'event' => :this_event))
31
31
  Reactor::Event.publish(:this_event, {random: 'data'})
32
32
  end
33
33
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: reactor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - winfred
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-07-15 00:00:00.000000000 Z
12
+ date: 2013-07-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler