cony 1.3.0 → 1.4.0

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.
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- cony (1.3.0)
4
+ cony (1.4.0)
5
5
  activesupport (>= 3)
6
6
  bunny (~> 1.1.7)
7
7
 
data/README.md CHANGED
@@ -98,7 +98,9 @@ The sent JSON structure will look like this:
98
98
  ```json
99
99
  {
100
100
  "id": 1337,
101
- "changes": [],
101
+ "changes": [
102
+ { "name": { "old": "value", "new": null } }
103
+ ],
102
104
  "event": "destroyed",
103
105
  "model": "Example::Model",
104
106
  }
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.3.0
1
+ 1.4.0
@@ -9,48 +9,46 @@ module Cony
9
9
  extend ActiveSupport::Concern
10
10
 
11
11
  included do
12
- after_create :cony_send_create_notify
13
- after_update :cony_send_update_notify
14
- after_destroy :cony_send_destroy_notify
12
+ after_create :cony_save_create_notify_data
13
+ after_update :cony_save_update_notify_data
14
+ after_destroy :cony_save_destroy_notify_data
15
+ after_commit :cony_publish
15
16
  end
16
17
 
17
- def cony_send_create_notify
18
- publish(:created, cony_changes_created)
18
+ def cony_save_create_notify_data
19
+ cony_prepare_notify :created, cony_changes_created
19
20
  end
20
21
 
21
- def cony_send_update_notify
22
- publish(:updated, cony_changes_updated)
22
+ def cony_save_update_notify_data
23
+ cony_prepare_notify :updated, cony_changes_updated
23
24
  end
24
25
 
25
- def cony_send_destroy_notify
26
- publish(:destroyed, cony_changes_destroyed)
26
+ def cony_save_destroy_notify_data
27
+ cony_prepare_notify :destroyed, cony_changes_destroyed
27
28
  end
28
29
 
29
-
30
- private
31
- def publish(type, cony_changes)
30
+ def cony_publish
32
31
  return if Cony.config.test_mode
33
- amqp_connection.publish(
34
- {id: self.id, changes: cony_changes, model: self.class.name, event: type},
35
- "#{self.class.name.underscore}.mutation.#{type}")
32
+ cony_amqp_connection.publish(cony_notify_hash, cony_notify_routing_key)
36
33
  end
37
34
 
38
- def amqp_connection
39
- @amqp_connection ||= Cony::AMQPConnectionHandler.new(Cony.config.amqp)
35
+ private
36
+ def cony_amqp_connection
37
+ @cony_amqp_connection ||= Cony::AMQPConnectionHandler.new(Cony.config.amqp)
40
38
  end
41
39
 
42
- def mapped_changes
40
+ def cony_mapped_changes
43
41
  changes.map do |name, change|
44
42
  {name => {old: change.first, new: change.last}}
45
43
  end
46
44
  end
47
45
 
48
46
  def cony_changes_created
49
- mapped_changes
47
+ cony_mapped_changes
50
48
  end
51
49
 
52
50
  def cony_changes_updated
53
- mapped_changes
51
+ cony_mapped_changes
54
52
  end
55
53
 
56
54
  def cony_changes_destroyed
@@ -58,5 +56,17 @@ module Cony
58
56
  {name => {old: value, new: nil}}
59
57
  end
60
58
  end
59
+
60
+ def cony_notify_hash
61
+ {id: self.id, changes: @cony_notify[:changes], model: self.class.name, event: @cony_notify[:event]}
62
+ end
63
+
64
+ def cony_notify_routing_key
65
+ "#{self.class.name.underscore}.mutation.#{@cony_notify[:event]}"
66
+ end
67
+
68
+ def cony_prepare_notify(event, changes)
69
+ @cony_notify = { event: event, changes: changes }
70
+ end
61
71
  end
62
72
  end
@@ -24,6 +24,7 @@ describe Cony::ActiveRecord do
24
24
  def self.after_create(callback); end
25
25
  def self.after_update(callback); end
26
26
  def self.after_destroy(callback); end
27
+ def self.after_commit(callback); end
27
28
  def self.name; "Anonymaus::Klass"; end
28
29
  def id; #{id}; end
29
30
  def changes; #{active_record_changes}; end
@@ -44,7 +45,8 @@ describe Cony::ActiveRecord do
44
45
  let(:event) { :created }
45
46
  it 'uses the amqp connection to send the notify' do
46
47
  amqp_connection.should_receive(:publish).with(expected_payload, 'anonymaus/klass.mutation.created')
47
- subject.cony_send_create_notify
48
+ subject.cony_save_create_notify_data
49
+ subject.cony_publish
48
50
  end
49
51
  end
50
52
 
@@ -52,7 +54,8 @@ describe Cony::ActiveRecord do
52
54
  let(:event) { :updated }
53
55
  it 'uses the amqp connection to send the notify' do
54
56
  amqp_connection.should_receive(:publish).with(expected_payload, 'anonymaus/klass.mutation.updated')
55
- subject.cony_send_update_notify
57
+ subject.cony_save_update_notify_data
58
+ subject.cony_publish
56
59
  end
57
60
  end
58
61
 
@@ -61,7 +64,8 @@ describe Cony::ActiveRecord do
61
64
  let(:cony_changes) { [{name: {old: 'value', new: nil}}] }
62
65
  it 'uses the amqp connection to send the notify' do
63
66
  amqp_connection.should_receive(:publish).with(expected_payload, 'anonymaus/klass.mutation.destroyed')
64
- subject.cony_send_destroy_notify
67
+ subject.cony_save_destroy_notify_data
68
+ subject.cony_publish
65
69
  end
66
70
  end
67
71
 
@@ -71,7 +75,8 @@ describe Cony::ActiveRecord do
71
75
  end
72
76
  it 'does not send the message' do
73
77
  expect(Cony::AMQPConnectionHandler).to_not receive(:new)
74
- subject.cony_send_create_notify
78
+ subject.cony_save_create_notify_data
79
+ subject.cony_publish
75
80
  end
76
81
  end
77
82
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cony
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-04-22 00:00:00.000000000 Z
12
+ date: 2014-04-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -120,7 +120,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
120
120
  version: '0'
121
121
  segments:
122
122
  - 0
123
- hash: -395064848044904818
123
+ hash: 1642476899976507358
124
124
  required_rubygems_version: !ruby/object:Gem::Requirement
125
125
  none: false
126
126
  requirements:
@@ -129,7 +129,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
129
129
  version: '0'
130
130
  segments:
131
131
  - 0
132
- hash: -395064848044904818
132
+ hash: 1642476899976507358
133
133
  requirements: []
134
134
  rubyforge_project:
135
135
  rubygems_version: 1.8.23