reactor 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3f30c15c4574595a8879b7dd9a6058605dad0ad1
4
- data.tar.gz: 14ae0caff4f4487d87be51e55e0408811ca7f576
3
+ metadata.gz: b8f973cd9837cbb3928aed292f258b3de46a3db6
4
+ data.tar.gz: ca362f7c430d3255d78268fd5a1db0a056a5fc54
5
5
  SHA512:
6
- metadata.gz: 74d22941c9ead4ab5ec852d77f9ffce49c0045947a9ed993cfabf4ab47b98ff225db226566458faa74f6c4ea73071aec62c10eb24f1724835cae4dd87b7f50a5
7
- data.tar.gz: 90e8a390c1128c75e3bb93bd35e47c1b16e3d784bb540f59a9c5b787318058aba0d592cf6c34174360fba11d6db74665c0966cfa8c8c993d9116a997ef9e7a34
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
- Reactor::Event.delay.publish name, data
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
@@ -1,3 +1,3 @@
1
1
  module Reactor
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
@@ -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
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.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - winfred