reactor 0.2.10 → 0.3.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: 3b3832777ade1de29503fe30b12109e238702e68
4
- data.tar.gz: 74107e0eba5012638fee991e573050a51790e656
3
+ metadata.gz: ec3412b610bacc15fa57793c615761da1191b283
4
+ data.tar.gz: 0f8613b3601c6573dc6d6c15520e9ed0496f9d25
5
5
  SHA512:
6
- metadata.gz: 7236791284614629ec715b6f6ca1c93be6d9b6e1aafe03787e0f5918706786c96b729ad2be8324a0ace626863d5dc3b7be39aa2415b527ab8f8b41944fdc3bc5
7
- data.tar.gz: afe337ddb5c6bfb53334c4c28a30a7bb8dd10a5a31898f6baa6bcfe42dbeb64ba8f561b7ba7d291611aabe4c2b536c173391c3194fdb9dd96ab3597f4bbda1fe
6
+ metadata.gz: a9d392a21a36ebf60fb6e07cb2505f1053dddd96a447a5d06b3b1595707e37ef716379e66d13dd56c2eaf926d5b612678d68fa159297d907eac6a7c166634568
7
+ data.tar.gz: 76a5d911946cbf24865daa12cddac19a627c1b4d3478b739e43f396bf64672f71b80881c9e643c6e9c8547ab6af7b005628d539de99e97508442540034652cd5
data/README.md CHANGED
@@ -1,6 +1,14 @@
1
1
  # Reactor
2
2
 
3
- TODO: Write a gem description
3
+ Warning: this is under active development!
4
+
5
+ This gem aims to provide the following tools to augment your ActiveRecord & Sidekiq stack.
6
+
7
+ 1. Barebones event API through Sidekiq to publish whatever you want
8
+ 2. Database-driven API to manage subscribers so that users may rewire whatever you let them (transactional emails, campaigns, etc...)
9
+ 3. Static/Code-driven API to subscribe a basic ruby block to an event.
10
+ 4. A new communication pattern between your ActiveRecord models that runs asynchronously through Sidekiq.
11
+ a. describe model lifecycle events and callbacks with class-level helper methods/DSL
4
12
 
5
13
  ## Installation
6
14
 
@@ -18,7 +26,31 @@ Or install it yourself as:
18
26
 
19
27
  ## Usage
20
28
 
21
- TODO: Write usage instructions here
29
+ Well, this is evolving, so it's probably best to go read the specs.
30
+
31
+
32
+ ### Barebones API
33
+
34
+ Event.publish(:event_name, any: 'data', you: 'want')
35
+
36
+ ### ActiveModel extensions
37
+
38
+ #### Publishable
39
+
40
+ Describe lifecycle events like so
41
+
42
+ publishes :my_model_created
43
+ publishes :state_has_changed, if: -> { state_has_changed? }
44
+
45
+ #### Subscribable
46
+
47
+ *New in 0.3*
48
+
49
+ You can now bind any block to an event in your models like so
50
+
51
+ subscribes_to :any_event do |event|
52
+ MyModel.find(event.target).do_something_about_it!
53
+ end
22
54
 
23
55
  ## Contributing
24
56
 
data/lib/reactor/event.rb CHANGED
@@ -55,6 +55,12 @@ class Reactor::Event
55
55
  Reactor::Subscriber.where(event: '*').each do |s|
56
56
  Reactor::Subscriber.delay.fire s.id, data
57
57
  end
58
+
59
+ if (static_subscribers = Reactor::STATIC_SUBSCRIBERS[name.to_s] || []).any?
60
+ static_subscribers.each do |callback|
61
+ callback.call(Reactor::Event.new(data.merge(event: name.to_s)))
62
+ end
63
+ end
58
64
  end
59
65
 
60
66
  private
@@ -1,4 +1,4 @@
1
- module Reactor::Eventable
1
+ module Reactor::Publishable
2
2
  extend ActiveSupport::Concern
3
3
 
4
4
  included do
@@ -0,0 +1,9 @@
1
+ module Reactor::Subscribable
2
+ extend ActiveSupport::Concern
3
+
4
+ module ClassMethods
5
+ def subscribes_to(event, &callback)
6
+ (Reactor::STATIC_SUBSCRIBERS[event.to_s] ||= []).push(callback)
7
+ end
8
+ end
9
+ end
@@ -23,4 +23,12 @@ RSpec::Matchers.define :publish_events do |*events|
23
23
  end
24
24
  block.call
25
25
  end
26
+ end
27
+
28
+ RSpec::Matchers.define :subscribe_to do |name, data = {}, &expectations|
29
+
30
+ match do
31
+ expectations.call if expectations.present?
32
+ Reactor::Event.publish(name, data)
33
+ end
26
34
  end
@@ -1,3 +1,3 @@
1
1
  module Reactor
2
- VERSION = "0.2.10"
2
+ VERSION = "0.3.0"
3
3
  end
data/lib/reactor.rb CHANGED
@@ -1,11 +1,13 @@
1
1
  require "reactor/version"
2
- require "reactor/models/concerns/eventable"
2
+ require "reactor/models/concerns/publishable"
3
+ require "reactor/models/concerns/subscribable"
3
4
  require "reactor/models/concerns/optionally_subclassable"
4
5
  require "reactor/models/subscriber"
5
6
  require "reactor/event"
6
7
 
7
8
  module Reactor
8
-
9
+ STATIC_SUBSCRIBERS = {}
9
10
  end
10
11
 
11
- ActiveRecord::Base.send(:include, Reactor::Eventable)
12
+ ActiveRecord::Base.send(:include, Reactor::Publishable)
13
+ ActiveRecord::Base.send(:include, Reactor::Subscribable)
@@ -30,7 +30,7 @@ class TestSubscriber < Reactor::Subscriber
30
30
  end
31
31
  end
32
32
 
33
- describe Reactor::Eventable do
33
+ describe Reactor::Publishable do
34
34
  before { TestSubscriber.destroy_all }
35
35
  describe 'publish' do
36
36
  let(:pet) { Pet.create! }
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+
4
+ class Auction < ActiveRecord::Base
5
+ subscribes_to :bid_made do |event|
6
+ event.target.update_column :status, 'first_bid_made'
7
+ end
8
+ end
9
+
10
+ describe Reactor::Subscribable do
11
+
12
+ describe 'subscribes_to' do
13
+ it 'binds block of code statically to event being fired' do
14
+ Auction.any_instance.should_receive(:update_column).with(:status, 'first_bid_made')
15
+ Reactor::Event.publish(:bid_made, target: Auction.create)
16
+ end
17
+ end
18
+ 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.2.10
4
+ version: 0.3.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-09 00:00:00.000000000 Z
12
+ date: 2013-07-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -70,14 +70,16 @@ files:
70
70
  - Rakefile
71
71
  - lib/reactor.rb
72
72
  - lib/reactor/event.rb
73
- - lib/reactor/models/concerns/eventable.rb
74
73
  - lib/reactor/models/concerns/optionally_subclassable.rb
74
+ - lib/reactor/models/concerns/publishable.rb
75
+ - lib/reactor/models/concerns/subscribable.rb
75
76
  - lib/reactor/models/subscriber.rb
76
77
  - lib/reactor/testing/matchers.rb
77
78
  - lib/reactor/version.rb
78
79
  - reactor.gemspec
79
80
  - spec/event_spec.rb
80
- - spec/models/concerns/eventable_spec.rb
81
+ - spec/models/concerns/publishable_spec.rb
82
+ - spec/models/concerns/subscribable_spec.rb
81
83
  - spec/models/subscriber_spec.rb
82
84
  - spec/spec_helper.rb
83
85
  - spec/support/active_record.rb
@@ -107,7 +109,8 @@ specification_version: 4
107
109
  summary: Sidekiq/ActiveRecord pubsub lib
108
110
  test_files:
109
111
  - spec/event_spec.rb
110
- - spec/models/concerns/eventable_spec.rb
112
+ - spec/models/concerns/publishable_spec.rb
113
+ - spec/models/concerns/subscribable_spec.rb
111
114
  - spec/models/subscriber_spec.rb
112
115
  - spec/spec_helper.rb
113
116
  - spec/support/active_record.rb