reactor 0.3.1 → 0.3.2

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: 569d1ffc0633f47aa79a69447cdd3cf594baff00
4
- data.tar.gz: 4db89fd791f775285053f866ea5cf5b1c9a36699
3
+ metadata.gz: 0f1aac22b0919076019fe2d53dd7999309d25216
4
+ data.tar.gz: 0ea239c9e18da1b510ff0ef87971abc991425b9b
5
5
  SHA512:
6
- metadata.gz: d32b3d0df0c44393d8e82f5e53010529e19a63dfd6ba54ac976c290b9c8d8f44d241a6f1929cc3a83d125770fac57e063f8ab18d52c09ee934216b8dd7870156
7
- data.tar.gz: fd248e0bbede4171500af8e8de84edeb85d37ddca42b5650a49808f39e5186cc48385852d2b3e62051e457cf120f062f54b56862ac7ccf5c8f05c682cf7224dd
6
+ metadata.gz: 2ef2dabe5ccedc03b97990c583a992c6145716cd775d90fada6540a4b9395195e677db3e135d159240830be6701871a5615cbe966f769e3a4568b936bf925168
7
+ data.tar.gz: 81f0d2605e65e50f3df59b2fe79f29e6ff327e8c658e6d9d6c6ee3e39bed8a0fbdb775551a33b7e888e68cd5efc21e6c44a32943eba30acf593bafc120a83d8a
data/README.md CHANGED
@@ -48,7 +48,7 @@ Well, this is evolving, so it's probably best to go read the specs.
48
48
 
49
49
  You can now bind any block to an event in your models like so
50
50
 
51
- subscribes_to :any_event do |event|
51
+ on_event :any_event do |event|
52
52
  MyModel.find(event.target).do_something_about_it!
53
53
  end
54
54
 
data/lib/reactor/event.rb CHANGED
@@ -56,9 +56,14 @@ class Reactor::Event
56
56
  Reactor::Subscriber.delay.fire s.id, data
57
57
  end
58
58
 
59
- if (static_subscribers = Reactor::STATIC_SUBSCRIBERS[name.to_s] || []).any?
59
+ if (static_subscribers = Reactor::SUBSCRIBERS[name.to_s] || []).any?
60
60
  static_subscribers.each do |callback|
61
- callback.call(Reactor::Event.new(data.merge(event: name.to_s)))
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))
66
+ end
62
67
  end
63
68
  end
64
69
  end
@@ -2,8 +2,15 @@ module Reactor::Subscribable
2
2
  extend ActiveSupport::Concern
3
3
 
4
4
  module ClassMethods
5
- def on_event(event, &callback)
6
- (Reactor::STATIC_SUBSCRIBERS[event.to_s] ||= []).push(callback)
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
13
+ (Reactor::SUBSCRIBERS[event.to_s] ||= []).push(callback)
7
14
  end
8
15
  end
9
16
  end
@@ -1,3 +1,3 @@
1
1
  module Reactor
2
- VERSION = "0.3.1"
2
+ VERSION = "0.3.2"
3
3
  end
data/lib/reactor.rb CHANGED
@@ -6,7 +6,7 @@ require "reactor/models/subscriber"
6
6
  require "reactor/event"
7
7
 
8
8
  module Reactor
9
- STATIC_SUBSCRIBERS = {}
9
+ SUBSCRIBERS = {}
10
10
  end
11
11
 
12
12
  ActiveRecord::Base.send(:include, Reactor::Publishable)
@@ -5,14 +5,31 @@ class Auction < ActiveRecord::Base
5
5
  on_event :bid_made do |event|
6
6
  event.target.update_column :status, 'first_bid_made'
7
7
  end
8
+
9
+ on_event :puppy_delivered, :ring_bell
10
+ on_event :any_event, -> (event) { puppies! }
11
+
12
+ def self.ring_bell(event)
13
+ pp "ring ring! #{event}"
14
+ end
8
15
  end
9
16
 
10
17
  describe Reactor::Subscribable do
11
18
 
12
- describe 'subscribes_to' do
19
+ describe 'on_event' do
13
20
  it 'binds block of code statically to event being fired' do
14
21
  Auction.any_instance.should_receive(:update_column).with(:status, 'first_bid_made')
15
22
  Reactor::Event.publish(:bid_made, target: Auction.create)
16
23
  end
24
+
25
+ it 'binds symbol to class method' do
26
+ Auction.should_receive(:ring_bell)
27
+ Reactor::Event.publish(:puppy_delivered)
28
+ end
29
+
30
+ it 'binds proc' do
31
+ Auction.should_receive(:puppies!)
32
+ Reactor::Event.publish(:any_event)
33
+ end
17
34
  end
18
35
  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.1
4
+ version: 0.3.2
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-13 00:00:00.000000000 Z
12
+ date: 2013-07-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler