reactor 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/reactor/models/concerns/eventable.rb +19 -1
- data/lib/reactor/version.rb +1 -1
- data/spec/models/concerns/eventable_spec.rb +16 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b8f973cd9837cbb3928aed292f258b3de46a3db6
|
4
|
+
data.tar.gz: ca362f7c430d3255d78268fd5a1db0a056a5fc54
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cf86502c96dcaa4e866a9c8bc8e21412a3f9ac53a37409f55dd6a793fdfcd67f0af5d34b5d70dab30efccd989fd8b63058ad0336511e7c7fa8f386cc38ca5a75
|
7
|
+
data.tar.gz: 2f3a5f5ad4573a2668d96f952344554542a48be8b172fe2a2c210bb85c38fd330ff33d8cbb2dd0845dc4e06e50cd6c110168e8eaddf79ebd0a59cd1fc783b3d9
|
@@ -27,7 +27,15 @@ module Reactor::Eventable
|
|
27
27
|
data = data.merge(
|
28
28
|
at: ( data[:at] ? send(data[:at]) : nil), actor: self
|
29
29
|
).except(:watch)
|
30
|
-
|
30
|
+
need_to_fire = case (ifarg = data[:if])
|
31
|
+
when Proc
|
32
|
+
what = instance_exec &ifarg
|
33
|
+
when Symbol
|
34
|
+
send(ifarg)
|
35
|
+
else
|
36
|
+
true
|
37
|
+
end
|
38
|
+
Reactor::Event.delay.publish name, data if need_to_fire
|
31
39
|
end
|
32
40
|
end
|
33
41
|
|
@@ -39,6 +47,16 @@ module Reactor::Eventable
|
|
39
47
|
actor: self,
|
40
48
|
was: send("#{data[:at]}_was")
|
41
49
|
end
|
50
|
+
|
51
|
+
if data[:if]
|
52
|
+
need_to_fire = case (ifarg = data[:if])
|
53
|
+
when Proc
|
54
|
+
instance_exec &ifarg
|
55
|
+
when Symbol
|
56
|
+
send(ifarg)
|
57
|
+
end
|
58
|
+
Reactor::Event.delay.publish name, actor: self if need_to_fire
|
59
|
+
end
|
42
60
|
end
|
43
61
|
end
|
44
62
|
end
|
data/lib/reactor/version.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
class Auction < ActiveRecord::Base
|
4
|
+
attr_accessor :we_want_it
|
5
|
+
|
4
6
|
def ring_timeout
|
5
7
|
created_at + 30.seconds
|
6
8
|
end
|
@@ -11,9 +13,11 @@ class Auction < ActiveRecord::Base
|
|
11
13
|
|
12
14
|
publishes :bell
|
13
15
|
publishes :ring, at: :ring_timeout, watch: :name
|
16
|
+
publishes :conditional_event_on_save, if: -> { we_want_it }
|
14
17
|
end
|
15
18
|
|
16
19
|
class TestSubscriber < Reactor::Subscriber
|
20
|
+
@@called = false
|
17
21
|
|
18
22
|
on_fire do
|
19
23
|
@@called = true
|
@@ -43,5 +47,17 @@ describe Reactor::Eventable do
|
|
43
47
|
auction.save
|
44
48
|
TestSubscriber.class_variable_get(:@@called).should be_false
|
45
49
|
end
|
50
|
+
|
51
|
+
it 'can fire events onsave for any condition' do
|
52
|
+
TestSubscriber.create! event: :conditional_event_on_save
|
53
|
+
auction
|
54
|
+
TestSubscriber.class_variable_set(:@@called, false)
|
55
|
+
auction.start_at = 1.day.from_now
|
56
|
+
auction.save
|
57
|
+
TestSubscriber.class_variable_get(:@@called).should be_false
|
58
|
+
auction.we_want_it = true
|
59
|
+
auction.save
|
60
|
+
TestSubscriber.class_variable_get(:@@called).should be_true
|
61
|
+
end
|
46
62
|
end
|
47
63
|
end
|