reactor 0.0.1 → 0.1.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: c1ed56da3f6b079d59b295ce74da1cf587ebf593
4
- data.tar.gz: 41a58ff5ed78311cbf680b64732be8b4e6ea252d
3
+ metadata.gz: 0a4ebfd7804d09982d8a0f41393dc6f66e9631fb
4
+ data.tar.gz: 64432277671680520c27306e2bc45c5e5c58b044
5
5
  SHA512:
6
- metadata.gz: d8fa857f03b7205b53f8931355c90ec261a43c45f20c5726709f3afa9197a4598c54262724da35cd26004c9da49bd9395ae3563201e2a9080078daf1e73ee4f4
7
- data.tar.gz: 50c9fd5ebdb5aa978966ea0f6418b086dd0607dd5c8304c31fc0528198e56a70e1b60947d69af6698dcf909f3804f152f6d34880bff3cdd1cd535e703270780a
6
+ metadata.gz: c813a25c6be9cb5fba1cfa19adeb00d4573026d3dd60e574450dd834bb54c6a904a9d3c0c5b5a9a6273dc19061ec31050dc363f64ca57ea67b5b40ea5564d976
7
+ data.tar.gz: 802c5f3fa50a9e8a5dfa3f051c682b8a208c6a67b1b8109c15d15be92c214652dca1400c26050e510ee99fa180ecfa133ab59342b21ec5b93d21ab2dfe44c623
data/lib/reactor/event.rb CHANGED
@@ -1,8 +1,29 @@
1
1
  class Reactor::Event
2
2
  include Reactor::OptionallySubclassable
3
3
 
4
+ attr_accessor :data
5
+
6
+ def initialize(data = {})
7
+ self.data = {}.with_indifferent_access
8
+ data.each do |key, value|
9
+ self.send("#{key}=", value)
10
+ end
11
+ end
12
+
13
+ def method_missing(method, *args)
14
+ if method.to_s.include?('=')
15
+ try_setter(method, *args)
16
+ else
17
+ try_getter(method)
18
+ end
19
+ end
20
+
21
+ def to_s
22
+ name
23
+ end
24
+
4
25
  def self.publish(name, data = {})
5
- message = Reactor::Message.new(data.merge(event: name))
26
+ message = new(data.merge(event: name))
6
27
  if (message.at)
7
28
  delay_until(message.at).process name, message.data
8
29
  else
@@ -18,10 +39,6 @@ class Reactor::Event
18
39
  delay.publish(name, data.except(:was)) if data[:at].future?
19
40
  end
20
41
 
21
- def to_s
22
- name
23
- end
24
-
25
42
  def self.process(name, data)
26
43
  Reactor::Subscriber.where(event: name.to_s).each do |subscriber|
27
44
  Reactor::Subscriber.delay.fire subscriber.id, data
@@ -46,4 +63,29 @@ class Reactor::Event
46
63
  def self.remove_scheduled_job(job)
47
64
  Sidekiq.redis { |r| r.zrem 'schedule', MultiJson.encode(job) }
48
65
  end
66
+
67
+ def try_setter(method, object, *args)
68
+ if object.is_a? ActiveRecord::Base
69
+ send("#{method}_id", object.id)
70
+ send("#{method}_type", object.class.to_s)
71
+ else
72
+ data[method.to_s.gsub('=','')] = object
73
+ end
74
+ end
75
+
76
+ def try_getter(method)
77
+ if polymorphic_association? method
78
+ initialize_polymorphic_association method
79
+ elsif data.has_key?(method)
80
+ data[method]
81
+ end
82
+ end
83
+
84
+ def polymorphic_association?(method)
85
+ data.has_key?("#{method}_type")
86
+ end
87
+
88
+ def initialize_polymorphic_association(method)
89
+ data["#{method}_type"].constantize.find(data["#{method}_id"])
90
+ end
49
91
  end
@@ -7,7 +7,7 @@ class Reactor::Subscriber < ActiveRecord::Base
7
7
  end
8
8
 
9
9
  def fire(data)
10
- self.message = Reactor::Message.new(data)
10
+ self.message = Reactor::Event.new(data)
11
11
  instance_exec &self.class.on_fire
12
12
  self
13
13
  end
@@ -1,3 +1,3 @@
1
1
  module Reactor
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
data/lib/reactor.rb CHANGED
@@ -3,7 +3,6 @@ require "reactor/models/concerns/eventable"
3
3
  require "reactor/models/concerns/optionally_subclassable"
4
4
  require "reactor/models/subscriber"
5
5
  require "reactor/event"
6
- require "reactor/message"
7
6
 
8
7
  module Reactor
9
8
 
data/spec/event_spec.rb CHANGED
@@ -1,5 +1,17 @@
1
1
  require 'spec_helper'
2
2
 
3
+ module MyModule
4
+ class Pet < ActiveRecord::Base
5
+ end
6
+
7
+ class Cat < Pet
8
+ end
9
+ end
10
+
11
+ class ArbitraryModel < ActiveRecord::Base
12
+ end
13
+
14
+
3
15
  describe Reactor::Event do
4
16
 
5
17
  let(:event_name) { :user_did_this }
@@ -18,4 +30,51 @@ describe Reactor::Event do
18
30
  Reactor::Event.process(event_name, actor_id: '1')
19
31
  end
20
32
  end
33
+
34
+ describe 'event content' do
35
+ let(:cat) { MyModule::Cat.create }
36
+ let(:arbitrary_model) { ArbitraryModel.create }
37
+ let(:event_data) { {random: 'data', pet_id: cat.id, pet_type: cat.class.to_s, arbitrary_model: arbitrary_model } }
38
+ let(:event) { Reactor::Event.new(event_data) }
39
+
40
+ describe 'data key fallthrough' do
41
+ subject { event }
42
+
43
+ describe 'getters' do
44
+ context 'basic key value' do
45
+ its(:random) { should == 'data' }
46
+ end
47
+
48
+ context 'foreign key and foreign type' do
49
+ its(:pet) { should be_a MyModule::Cat }
50
+ its('pet.id') { should == MyModule::Cat.last.id }
51
+ end
52
+ end
53
+
54
+ describe 'setters' do
55
+ it 'sets simple keys' do
56
+ event.simple = 'key'
57
+ event.data[:simple].should == 'key'
58
+ end
59
+
60
+ it 'sets active_record polymorphic keys' do
61
+ event.complex = cat = MyModule::Cat.create
62
+ event.complex_id = cat.id
63
+ event.complex_type = cat.class.to_s
64
+ end
65
+ end
66
+ end
67
+
68
+ describe 'data' do
69
+ let(:serialized_event) { event.data }
70
+ specify { serialized_event.should be_a Hash }
71
+ specify { serialized_event[:random].should == 'data' }
72
+ end
73
+
74
+ describe 'new' do
75
+ specify { event.should be_a Reactor::Event }
76
+ specify { event.pet_id.should == cat.id }
77
+ specify { event.arbitrary_model_id.should == arbitrary_model.id }
78
+ end
79
+ end
21
80
  end
@@ -13,7 +13,7 @@ describe Reactor::Subscriber do
13
13
  describe 'fire' do
14
14
  subject { MySubscriber.create(event: :you_name_it).fire some: 'random', event: 'data' }
15
15
 
16
- its(:message) { should be_a Reactor::Message }
16
+ its(:message) { should be_a Reactor::Event }
17
17
  its('message.some') { should == 'random' }
18
18
 
19
19
  it 'executes block given' do
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.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - winfred
@@ -70,7 +70,6 @@ files:
70
70
  - Rakefile
71
71
  - lib/reactor.rb
72
72
  - lib/reactor/event.rb
73
- - lib/reactor/message.rb
74
73
  - lib/reactor/models/concerns/eventable.rb
75
74
  - lib/reactor/models/concerns/optionally_subclassable.rb
76
75
  - lib/reactor/models/subscriber.rb
@@ -78,7 +77,6 @@ files:
78
77
  - lib/reactor/version.rb
79
78
  - reactor.gemspec
80
79
  - spec/event_spec.rb
81
- - spec/message_spec.rb
82
80
  - spec/models/concerns/eventable_spec.rb
83
81
  - spec/models/subscriber_spec.rb
84
82
  - spec/spec_helper.rb
@@ -109,7 +107,6 @@ specification_version: 4
109
107
  summary: Sidekiq/ActiveRecord pubsub lib
110
108
  test_files:
111
109
  - spec/event_spec.rb
112
- - spec/message_spec.rb
113
110
  - spec/models/concerns/eventable_spec.rb
114
111
  - spec/models/subscriber_spec.rb
115
112
  - spec/spec_helper.rb
@@ -1,46 +0,0 @@
1
- class Reactor::Message
2
- attr_accessor :data
3
-
4
- def initialize(data = {})
5
- self.data = {}.with_indifferent_access
6
- data.each do |key, value|
7
- self.send("#{key}=", value)
8
- end
9
- end
10
-
11
- def method_missing(method, *args)
12
- if method.to_s.include?('=')
13
- try_setter(method, *args)
14
- else
15
- try_getter(method)
16
- end
17
- end
18
-
19
- private
20
-
21
- def try_setter(method, object, *args)
22
- if object.is_a? ActiveRecord::Base
23
- send("#{method}_id", object.id)
24
- send("#{method}_type", object.class.to_s)
25
- else
26
- data[method.to_s.gsub('=','')] = object
27
- end
28
- end
29
-
30
- def try_getter(method)
31
- if polymorphic_association? method
32
- initialize_polymorphic_association method
33
- elsif data.has_key?(method)
34
- data[method]
35
- end
36
- end
37
-
38
- def polymorphic_association?(method)
39
- data.has_key?("#{method}_type")
40
- end
41
-
42
- def initialize_polymorphic_association(method)
43
- data["#{method}_type"].constantize.find(data["#{method}_id"])
44
- end
45
-
46
- end
data/spec/message_spec.rb DELETED
@@ -1,61 +0,0 @@
1
- require 'spec_helper'
2
- require 'support/active_record'
3
-
4
- module MyModule
5
- class Pet < ActiveRecord::Base
6
- end
7
-
8
- class Cat < Pet
9
- end
10
- end
11
-
12
-
13
- class ArbitraryModel < ActiveRecord::Base
14
- end
15
-
16
- describe Reactor::Message do
17
- let(:cat) { MyModule::Cat.create }
18
- let(:arbitrary_model) { ArbitraryModel.create }
19
- let(:message_data) { {random: 'data', pet_id: cat.id, pet_type: cat.class.to_s, arbitrary_model: arbitrary_model } }
20
- let(:message) { Reactor::Message.new(message_data) }
21
-
22
- describe 'data key fallthrough' do
23
- subject { message }
24
-
25
- describe 'getters' do
26
- context 'basic key value' do
27
- its(:random) { should == 'data' }
28
- end
29
-
30
- context 'foreign key and foreign type' do
31
- its(:pet) { should be_a MyModule::Cat }
32
- its('pet.id') { should == MyModule::Cat.last.id }
33
- end
34
- end
35
-
36
- describe 'setters' do
37
- it 'sets simple keys' do
38
- message.simple = 'key'
39
- message.data[:simple].should == 'key'
40
- end
41
-
42
- it 'sets active_record polymorphic keys' do
43
- message.complex = cat = MyModule::Cat.create
44
- message.complex_id = cat.id
45
- message.complex_type = cat.class.to_s
46
- end
47
- end
48
- end
49
-
50
- describe 'data' do
51
- let(:serialized_message) { message.data }
52
- specify { serialized_message.should be_a Hash }
53
- specify { serialized_message[:random].should == 'data' }
54
- end
55
-
56
- describe 'new' do
57
- specify { message.should be_a Reactor::Message }
58
- specify { message.pet_id.should == cat.id }
59
- specify { message.arbitrary_model_id.should == arbitrary_model.id }
60
- end
61
- end