reactor 0.1.0 → 0.1.1

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: 0a4ebfd7804d09982d8a0f41393dc6f66e9631fb
4
- data.tar.gz: 64432277671680520c27306e2bc45c5e5c58b044
3
+ metadata.gz: 3f30c15c4574595a8879b7dd9a6058605dad0ad1
4
+ data.tar.gz: 14ae0caff4f4487d87be51e55e0408811ca7f576
5
5
  SHA512:
6
- metadata.gz: c813a25c6be9cb5fba1cfa19adeb00d4573026d3dd60e574450dd834bb54c6a904a9d3c0c5b5a9a6273dc19061ec31050dc363f64ca57ea67b5b40ea5564d976
7
- data.tar.gz: 802c5f3fa50a9e8a5dfa3f051c682b8a208c6a67b1b8109c15d15be92c214652dca1400c26050e510ee99fa180ecfa133ab59342b21ec5b93d21ab2dfe44c623
6
+ metadata.gz: 74d22941c9ead4ab5ec852d77f9ffce49c0045947a9ed993cfabf4ab47b98ff225db226566458faa74f6c4ea73071aec62c10eb24f1724835cae4dd87b7f50a5
7
+ data.tar.gz: 90e8a390c1128c75e3bb93bd35e47c1b16e3d784bb540f59a9c5b787318058aba0d592cf6c34174360fba11d6db74665c0966cfa8c8c993d9116a997ef9e7a34
data/lib/reactor/event.rb CHANGED
@@ -40,6 +40,7 @@ class Reactor::Event
40
40
  end
41
41
 
42
42
  def self.process(name, data)
43
+ # fire database listeners
43
44
  Reactor::Subscriber.where(event: name.to_s).each do |subscriber|
44
45
  Reactor::Subscriber.delay.fire subscriber.id, data
45
46
  end
@@ -2,8 +2,8 @@ module Reactor::Eventable
2
2
  extend ActiveSupport::Concern
3
3
 
4
4
  included do
5
- after_create :schedule_events
6
- after_update :reschedule_events
5
+ after_commit :schedule_events, if: :persisted?, on: :create
6
+ after_commit :reschedule_events, if: :persisted?, on: :update
7
7
  end
8
8
 
9
9
  def publish(name, data = {})
@@ -24,15 +24,16 @@ module Reactor::Eventable
24
24
 
25
25
  def schedule_events
26
26
  self.class.events.each do |name, data|
27
- Reactor::Event.delay.publish name, data.merge(
28
- at: send(data[:at]), actor: self
27
+ data = data.merge(
28
+ at: ( data[:at] ? send(data[:at]) : nil), actor: self
29
29
  ).except(:watch)
30
+ Reactor::Event.delay.publish name, data
30
31
  end
31
32
  end
32
33
 
33
34
  def reschedule_events
34
35
  self.class.events.each do |name, data|
35
- if send("#{data[:watch] || data[:at]}_changed?")
36
+ if data[:at] && send("#{data[:watch] || data[:at]}_changed?")
36
37
  Reactor::Event.delay.reschedule name,
37
38
  at: send(data[:at]),
38
39
  actor: self,
@@ -1,3 +1,3 @@
1
1
  module Reactor
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -1,20 +1,47 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  class Auction < ActiveRecord::Base
4
- def ring_timeout(was: false)
5
- created_at + (was ? 10.seconds : 30.seconds)
4
+ def ring_timeout
5
+ created_at + 30.seconds
6
6
  end
7
7
 
8
+ def ring_timeout_was
9
+ created_at + 10.seconds
10
+ end
11
+
12
+ publishes :bell
8
13
  publishes :ring, at: :ring_timeout, watch: :name
9
14
  end
10
15
 
16
+ class TestSubscriber < Reactor::Subscriber
17
+
18
+ on_fire do
19
+ @@called = true
20
+ end
21
+ end
22
+
11
23
  describe Reactor::Eventable do
12
24
  describe 'publish' do
13
- let(:auction) { Auction.create }
25
+ let(:auction) { Auction.create! }
14
26
 
15
27
  it 'publishes an event with actor_id and actor_type set as self' do
16
- Reactor::Event.should_receive(:publish).with(:an_event, {what: 'the', actor: auction}).twice
28
+ auction
29
+ Reactor::Event.should_receive(:publish) do |name, data|
30
+ name.should == :an_event
31
+ data[:what].should == 'the'
32
+ data[:actor].should == auction
33
+ end
17
34
  auction.publish(:an_event, {what: 'the'})
18
35
  end
36
+
37
+ it 'supports immediate events (on create) that get fired once' do
38
+ TestSubscriber.create! event: :bell
39
+ auction
40
+ TestSubscriber.class_variable_get(:@@called).should be_true
41
+ TestSubscriber.class_variable_set(:@@called, false)
42
+ auction.start_at = 1.day.from_now
43
+ auction.save
44
+ TestSubscriber.class_variable_get(:@@called).should be_false
45
+ end
19
46
  end
20
47
  end
@@ -22,36 +22,6 @@ describe Reactor::Subscriber do
22
22
  end
23
23
 
24
24
  describe '.subscribes_to class helper' do
25
- #describe 'ensuring subscriber exists and is tied to event' do
26
- # it 'binds 1-1 when name given' do
27
- # expect {
28
- # MySubscriber.class_eval do
29
- # subscribes_to :event_times
30
- # end
31
- # }.to change { Reactor::Subscriber.count }.by(1)
32
- # end
33
- #
34
- # context 'binds to all when star is given' do
35
- # after { MySubscriber.destroy_all }
36
- #
37
- # it 'creates new subscriber' do
38
- # expect {
39
- # MySubscriber.class_eval do
40
- # subscribes_to '*'
41
- # end
42
- # }.to change { Reactor::Subscriber.count }.by(1)
43
- # end
44
- #
45
- # it 'doesnt create' do
46
- # MySubscriber.where(event: '*').first_or_create!
47
- # expect {
48
- # MySubscriber.class_eval do
49
- # subscribes_to '*'
50
- # end
51
- # }.to change { Reactor::Subscriber.count }.by(0)
52
- # end
53
- # end
54
- #end
55
25
  end
56
26
 
57
27
  describe 'matcher' do
data/spec/spec_helper.rb CHANGED
@@ -6,6 +6,7 @@ require 'support/active_record'
6
6
  require 'sidekiq'
7
7
  require 'sidekiq/testing/inline'
8
8
  require 'reactor'
9
+ require 'reactor/testing/matchers'
9
10
 
10
11
  Sidekiq.configure_server do |config|
11
12
  config.redis = { url: ENV["REDISTOGO_URL"] }
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.1.0
4
+ version: 0.1.1
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-01 00:00:00.000000000 Z
12
+ date: 2013-07-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler