emque-producing 1.0.0.beta5 → 1.0.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.
- checksums.yaml +4 -4
- data/README.md +31 -1
- data/lib/emque/producing/message/message_with_changeset.rb +50 -7
- data/lib/emque/producing/version.rb +1 -1
- data/spec/producing/message/message_spec.rb +59 -13
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7696c08ea749cf14c85f0ce963fc626038baa140
|
4
|
+
data.tar.gz: ea30e9981cb33b17abf705ee85a9d37a946a4180
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d3132e5cbce4b9c1fec5a428e5d7d202e6d56fa4e7f03946df231e9a73435713f49acf1d4a7de07aea1cc27fb8dc64ed58b907d65f76aa3ceb6c184789a26247
|
7
|
+
data.tar.gz: 3025a044fd1d2ed21200b3d8aa093bc8b340dd6637dd42b7c5ccc6cb86c60489c6ec3ec03a65a2c95d81445f004ba5f221090420ae16daf4d7c964c2ac81c48d
|
data/README.md
CHANGED
@@ -53,6 +53,36 @@ Or install it yourself as:
|
|
53
53
|
message = MyMessage.new({:first_property => 1, :another_property => "another"})
|
54
54
|
message.publish
|
55
55
|
|
56
|
+
# create a message class including changesets
|
57
|
+
class MyChangesetMessage
|
58
|
+
include Emque::Producing.message(:with_changeset => true)
|
59
|
+
topic "topic1"
|
60
|
+
message_type "mymessage.new"
|
61
|
+
|
62
|
+
# Need to override an attribute name in the changeset? Simply define
|
63
|
+
# the old name and new name here. This can be useful for ensuring
|
64
|
+
# messages are consistent across varying producers.
|
65
|
+
translate_changeset_attrs :old_attr_name => :new_attr_name
|
66
|
+
end
|
67
|
+
|
68
|
+
produced_message = TestMessageWithChangeset.new(
|
69
|
+
:updated => {:old_attr_name => "Event"},
|
70
|
+
:original => {:old_attr_name => "Game"}
|
71
|
+
)
|
72
|
+
json = produced_message.to_json
|
73
|
+
consumed_message = Oj.load(json)
|
74
|
+
expect(consumed_message["change_set"]).to eql(
|
75
|
+
{
|
76
|
+
"original"=>{"new_attr_name"=>"Game"},
|
77
|
+
"updated"=>{"new_attr_name"=>"Event"},
|
78
|
+
"delta"=>{
|
79
|
+
"new_attr_name"=>{
|
80
|
+
"original"=>"Game", "updated"=>"Event"
|
81
|
+
}
|
82
|
+
}
|
83
|
+
}
|
84
|
+
)
|
85
|
+
|
56
86
|
For a more thorough guide to creating new messages and/or message producing
|
57
87
|
applications, [please read the wiki
|
58
88
|
entry](https://github.com/teamsnap/emque-producing/wiki/Creating-New-Producing-Applications)
|
@@ -77,7 +107,7 @@ configuration option `publish_messages` to false like so:
|
|
77
107
|
Emque::Producing.configure do |c|
|
78
108
|
c.publish_messages = false
|
79
109
|
...other options
|
80
|
-
end
|
110
|
+
end
|
81
111
|
```
|
82
112
|
This will prevent Emque from actually attempting to make the connection to your
|
83
113
|
adapter which may be convenient in the case of CI environments.
|
@@ -1,7 +1,15 @@
|
|
1
1
|
module Emque
|
2
2
|
module Producing
|
3
3
|
module MessageWithChangeset
|
4
|
+
module ClassMethods
|
5
|
+
def translate_changeset_attrs(attrs = {})
|
6
|
+
@attrs_to_translate ||= {}
|
7
|
+
@attrs_to_translate.merge!(attrs)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
4
11
|
def self.included(base)
|
12
|
+
base.extend(ClassMethods)
|
5
13
|
base.send(:include, Emque::Producing::Message)
|
6
14
|
base.send(:attribute, :partition_key, String, :default => nil, :required => false)
|
7
15
|
base.send(:attribute, :change_set, Hash, :default => :build_change_set, :required => true)
|
@@ -9,10 +17,18 @@ module Emque
|
|
9
17
|
base.send(:private_attribute, :original)
|
10
18
|
end
|
11
19
|
|
20
|
+
def translated_attrs
|
21
|
+
self.class.instance_variable_get(:@attrs_to_translate)
|
22
|
+
end
|
23
|
+
|
12
24
|
def build_change_set
|
13
|
-
ChangesPayloadGenerator
|
14
|
-
|
15
|
-
|
25
|
+
ChangesPayloadGenerator.new(
|
26
|
+
{
|
27
|
+
:original => original,
|
28
|
+
:updated => updated,
|
29
|
+
:translated_attrs => translated_attrs
|
30
|
+
}
|
31
|
+
).execute
|
16
32
|
end
|
17
33
|
|
18
34
|
def build_id
|
@@ -27,18 +43,45 @@ module Emque
|
|
27
43
|
end
|
28
44
|
|
29
45
|
class ChangesPayloadGenerator
|
30
|
-
def initialize(
|
31
|
-
@original =
|
32
|
-
@updated =
|
46
|
+
def initialize(changeset_data = {})
|
47
|
+
@original = changeset_data[:original] || {}
|
48
|
+
@updated = changeset_data[:updated] || {}
|
49
|
+
@translated_attrs = changeset_data[:translated_attrs] || {}
|
33
50
|
end
|
34
51
|
|
35
52
|
def execute
|
53
|
+
translate_attrs if translated_attrs.any?
|
36
54
|
{:original => original, :updated => updated, :delta => delta}
|
37
55
|
end
|
38
56
|
|
39
57
|
private
|
40
58
|
|
41
|
-
attr_reader :original, :updated
|
59
|
+
attr_reader :original, :updated, :translated_attrs
|
60
|
+
|
61
|
+
def translate_attrs
|
62
|
+
@original = translate(original)
|
63
|
+
@updated = translate(updated)
|
64
|
+
end
|
65
|
+
|
66
|
+
def deep_copy(attr_set)
|
67
|
+
Oj.load(Oj.dump(attr_set))
|
68
|
+
end
|
69
|
+
|
70
|
+
def translate(attr_set)
|
71
|
+
deep_copy(attr_set).tap do |cloned_attrs|
|
72
|
+
stringified_attrs.each_pair do |old_name, new_name|
|
73
|
+
if cloned_attrs.key?(old_name)
|
74
|
+
cloned_attrs[new_name] = cloned_attrs.delete(old_name)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def stringified_attrs
|
81
|
+
{}.tap do |new_hash|
|
82
|
+
translated_attrs.each_pair { |k,v| new_hash[k.to_s] = v.to_s }
|
83
|
+
end
|
84
|
+
end
|
42
85
|
|
43
86
|
def delta
|
44
87
|
if original.empty?
|
@@ -3,16 +3,6 @@ require "virtus"
|
|
3
3
|
require "emque/producing/message/message"
|
4
4
|
require "emque/producing/message/message_with_changeset"
|
5
5
|
|
6
|
-
class TestMessageWithChangeset
|
7
|
-
include Emque::Producing.message(:with_changeset => true)
|
8
|
-
|
9
|
-
topic "queue"
|
10
|
-
message_type "queue.new"
|
11
|
-
|
12
|
-
attribute :test_id, Integer, :required => true, :default => :build_id
|
13
|
-
private_attribute :extra, String, :default => "value"
|
14
|
-
end
|
15
|
-
|
16
6
|
class TestMessageWithChangesetCustomBuildId
|
17
7
|
include Emque::Producing.message(:with_changeset => true)
|
18
8
|
|
@@ -53,10 +43,66 @@ class MessageNoType
|
|
53
43
|
topic "testing"
|
54
44
|
end
|
55
45
|
|
46
|
+
class TestMessageWithChangeset
|
47
|
+
include Emque::Producing.message(:with_changeset => true)
|
48
|
+
|
49
|
+
topic "queue"
|
50
|
+
message_type "queue.new"
|
51
|
+
|
52
|
+
attribute :test_id, Integer, :required => true, :default => :build_id
|
53
|
+
private_attribute :extra, String, :default => "value"
|
54
|
+
translate_changeset_attrs(
|
55
|
+
:type => :event_type, :date => :event_date, :not_an_attr => :still_not_an_attr
|
56
|
+
)
|
57
|
+
end
|
58
|
+
|
56
59
|
describe Emque::Producing::Message do
|
57
|
-
before
|
58
|
-
|
59
|
-
|
60
|
+
before { Emque::Producing.configure { |c| c.app_name = "my_app" } }
|
61
|
+
|
62
|
+
describe "translating changeset attr names" do
|
63
|
+
it "allows a client to change attr names in the changeset message" do
|
64
|
+
produced_message = TestMessageWithChangeset.new(
|
65
|
+
:test_id => 1,
|
66
|
+
:original => {"type" => "Game", "date" => "2000-01-02"},
|
67
|
+
:updated => {"type" => "Event", "date" => "2000-01-01"}
|
68
|
+
)
|
69
|
+
json = produced_message.to_json
|
70
|
+
consumed_message = Oj.load(json)
|
71
|
+
expect(consumed_message["change_set"]).to eql(
|
72
|
+
{
|
73
|
+
"original"=>{"event_type"=>"Game", "event_date"=>"2000-01-02"},
|
74
|
+
"updated"=>{"event_type"=>"Event", "event_date"=>"2000-01-01"},
|
75
|
+
"delta"=>{
|
76
|
+
"event_type"=>{
|
77
|
+
"original"=>"Game", "updated"=>"Event"
|
78
|
+
},
|
79
|
+
"event_date"=>{
|
80
|
+
"original"=>"2000-01-02", "updated"=>"2000-01-01"
|
81
|
+
}
|
82
|
+
}
|
83
|
+
}
|
84
|
+
)
|
85
|
+
end
|
86
|
+
|
87
|
+
it "does not add new attr if the old attr is not present" do
|
88
|
+
produced_message = TestMessageWithChangeset.new(
|
89
|
+
:test_id => 1,
|
90
|
+
:updated => {"type" => "Event"},
|
91
|
+
:original => {"type" => "Game"}
|
92
|
+
)
|
93
|
+
json = produced_message.to_json
|
94
|
+
consumed_message = Oj.load(json)
|
95
|
+
expect(consumed_message["change_set"]).to eql(
|
96
|
+
{
|
97
|
+
"original"=>{"event_type"=>"Game"},
|
98
|
+
"updated"=>{"event_type"=>"Event"},
|
99
|
+
"delta"=>{
|
100
|
+
"event_type"=>{
|
101
|
+
"original"=>"Game", "updated"=>"Event"
|
102
|
+
}
|
103
|
+
}
|
104
|
+
}
|
105
|
+
)
|
60
106
|
end
|
61
107
|
end
|
62
108
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: emque-producing
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.0
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Emily Dobervich
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2015-
|
12
|
+
date: 2015-04-22 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: oj
|
@@ -153,9 +153,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
153
153
|
version: 1.9.3
|
154
154
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
155
155
|
requirements:
|
156
|
-
- - "
|
156
|
+
- - ">="
|
157
157
|
- !ruby/object:Gem::Version
|
158
|
-
version:
|
158
|
+
version: '0'
|
159
159
|
requirements: []
|
160
160
|
rubyforge_project:
|
161
161
|
rubygems_version: 2.2.2
|