pub_sub_model_sync 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +10 -0
- data/Gemfile.lock +1 -1
- data/README.md +11 -11
- data/lib/pub_sub_model_sync/config.rb +1 -0
- data/lib/pub_sub_model_sync/message_processor.rb +0 -6
- data/lib/pub_sub_model_sync/publisher.rb +13 -4
- data/lib/pub_sub_model_sync/publisher_concern.rb +16 -13
- data/lib/pub_sub_model_sync/subscriber_concern.rb +11 -8
- data/lib/pub_sub_model_sync/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4eda619ab984f99ffe46cfef36fa380aaa6766c1564081dcc4e5e53e6f7976e0
|
4
|
+
data.tar.gz: dd056d897c96bac38a20c115fee501cbb1383038483f74849bbb0e7217568163
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 75796a135c2f820d89303c29c9bb6dc78eaa1e207391a07ba86a45f547a7007b4b00f767bb949d354c5a90c504b5d2d287619fd3106bca1dce71e75588ac6173
|
7
|
+
data.tar.gz: fe4829f970142bfea0089beb4e86a27c9d1e08049213372ae77f94b9d02d16752218d58e88b25ac1f34848931da3651e4405caa3a8932401c1410d7f04e3d0da
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,15 @@
|
|
1
1
|
# Change Log
|
2
2
|
|
3
|
+
# 0.1.4
|
4
|
+
- Add attribute aliases when publishing, ```ps_publish(['name:full_name', 'email'])```
|
5
|
+
- Ability to retrieve publisher/subscriber crud settings
|
6
|
+
|
7
|
+
# 0.1.3
|
8
|
+
- shorter publisher/subscriber methods: ps_msync_subscribe into ps_subscribe
|
9
|
+
|
10
|
+
# 0.1.2
|
11
|
+
- fix not found pub/sub library (buggy)
|
12
|
+
|
3
13
|
# 0.1.1
|
4
14
|
- Add rabbitmq pub/sub service support
|
5
15
|
- Reformat to support multiple pub/sub services
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -45,7 +45,7 @@ And then execute: $ bundle install
|
|
45
45
|
|
46
46
|
- Add publishers/subscribers to your models (See examples below)
|
47
47
|
|
48
|
-
- Start
|
48
|
+
- Start subscribers to listen for publishers (Only in the app that has subscribers)
|
49
49
|
```ruby
|
50
50
|
rake pub_sub_model_sync:start
|
51
51
|
```
|
@@ -53,14 +53,14 @@ And then execute: $ bundle install
|
|
53
53
|
|
54
54
|
## Examples
|
55
55
|
```ruby
|
56
|
-
# App 1
|
56
|
+
# App 1 (Publisher)
|
57
57
|
# attributes: name email age
|
58
58
|
class User < ActiveRecord::Base
|
59
59
|
include PubSubModelSync::PublisherConcern
|
60
60
|
ps_publish(%i[name email])
|
61
61
|
end
|
62
62
|
|
63
|
-
# App 2
|
63
|
+
# App 2 (Subscriber)
|
64
64
|
class User < ActiveRecord::Base
|
65
65
|
include PubSubModelSync::SubscriberConcern
|
66
66
|
ps_subscribe(%i[name])
|
@@ -72,24 +72,24 @@ class User < ActiveRecord::Base
|
|
72
72
|
end
|
73
73
|
|
74
74
|
# Samples
|
75
|
-
User.create(name: 'test user') # Review your App 2 to see the created user (only name will be saved)
|
75
|
+
User.create(name: 'test user', email: 'sample@gmail.com') # Review your App 2 to see the created user (only name will be saved)
|
76
76
|
User.ps_class_publish({ msg: 'Hello' }, action: :greeting) # User.greeting method (Class method) will be called in App2
|
77
77
|
```
|
78
78
|
|
79
79
|
## Advanced Example
|
80
80
|
```ruby
|
81
|
-
# App 1
|
81
|
+
# App 1 (Publisher)
|
82
82
|
class User < ActiveRecord::Base
|
83
83
|
self.table_name = 'publisher_users'
|
84
84
|
include PubSubModelSync::PublisherConcern
|
85
|
-
ps_publish(%i[name], actions: %i[update], as_klass: 'Client', id: :client_id)
|
85
|
+
ps_publish(%i[name:full_name email], actions: %i[update], as_klass: 'Client', id: :client_id)
|
86
86
|
|
87
87
|
def ps_skip_for?(_action)
|
88
88
|
false # here logic with action to skip push message
|
89
89
|
end
|
90
90
|
end
|
91
91
|
|
92
|
-
# App 2
|
92
|
+
# App 2 (Subscriber)
|
93
93
|
class User < ActiveRecord::Base
|
94
94
|
self.table_name = 'subscriber_users'
|
95
95
|
include PubSubModelSync::SubscriberConcern
|
@@ -102,8 +102,8 @@ class User < ActiveRecord::Base
|
|
102
102
|
end
|
103
103
|
```
|
104
104
|
|
105
|
-
## Testing
|
106
|
-
-
|
105
|
+
## Testing with RSpec
|
106
|
+
- Config: (spec/rails_helper.rb)
|
107
107
|
```ruby
|
108
108
|
|
109
109
|
# when using google service
|
@@ -163,9 +163,9 @@ end
|
|
163
163
|
|
164
164
|
There are two special methods to extract crud configuration settings (attrs, id, ...):
|
165
165
|
|
166
|
-
Subscribers: ```User.
|
166
|
+
Subscribers: ```User.ps_subscriber```
|
167
167
|
|
168
|
-
Publishers: ```User.
|
168
|
+
Publishers: ```User.ps_publisher```
|
169
169
|
|
170
170
|
Note: Inspect all configured listeners with:
|
171
171
|
``` PubSubModelSync::Config.listeners ```
|
@@ -40,7 +40,6 @@ module PubSubModelSync
|
|
40
40
|
|
41
41
|
# support for: create, update, destroy
|
42
42
|
def call_listener(listener)
|
43
|
-
listener_add_crud_settings(listener)
|
44
43
|
model = find_model(listener)
|
45
44
|
if attrs[:action].to_sym == :destroy
|
46
45
|
model.destroy!
|
@@ -74,11 +73,6 @@ module PubSubModelSync
|
|
74
73
|
end
|
75
74
|
end
|
76
75
|
|
77
|
-
def listener_add_crud_settings(listener)
|
78
|
-
model_class = listener[:klass].constantize
|
79
|
-
listener[:settings] = model_class.ps_subscriber_settings
|
80
|
-
end
|
81
|
-
|
82
76
|
def log(message, kind = :info)
|
83
77
|
PubSubModelSync::Config.log "#{message} ==> #{[data, attrs]}", kind
|
84
78
|
end
|
@@ -14,12 +14,10 @@ module PubSubModelSync
|
|
14
14
|
|
15
15
|
# @param settings (Hash): { attrs: [], as_klass: nil, id: nil }
|
16
16
|
def publish_model(model, action, settings = nil)
|
17
|
-
settings ||= model.class.
|
17
|
+
settings ||= model.class.ps_publisher_info(action)
|
18
18
|
attributes = build_model_attrs(model, action, settings)
|
19
19
|
data = {}
|
20
|
-
if action !=
|
21
|
-
data = model.as_json(only: settings[:attrs], methods: settings[:attrs])
|
22
|
-
end
|
20
|
+
data = build_model_data(model, settings[:attrs]) if action != :destroy
|
23
21
|
connector.publish(data.symbolize_keys, attributes)
|
24
22
|
end
|
25
23
|
|
@@ -34,6 +32,17 @@ module PubSubModelSync
|
|
34
32
|
|
35
33
|
private
|
36
34
|
|
35
|
+
def build_model_data(model, model_props)
|
36
|
+
source_props = model_props.map { |prop| prop.to_s.split(':').first }
|
37
|
+
data = model.as_json(only: source_props, methods: source_props)
|
38
|
+
aliased_props = model_props.select { |prop| prop.to_s.include?(':') }
|
39
|
+
aliased_props.each do |prop|
|
40
|
+
source, target = prop.to_s.split(':')
|
41
|
+
data[target] = data.delete(source)
|
42
|
+
end
|
43
|
+
data.symbolize_keys
|
44
|
+
end
|
45
|
+
|
37
46
|
def build_model_attrs(model, action, settings)
|
38
47
|
as_klass = (settings[:as_klass] || model.class.name).to_s
|
39
48
|
id_val = model.send(settings[:id] || :id)
|
@@ -16,32 +16,35 @@ module PubSubModelSync
|
|
16
16
|
# @param settings (Hash): { actions: nil, as_klass: nil, id: nil }
|
17
17
|
def ps_publish(attrs, settings = {})
|
18
18
|
actions = settings.delete(:actions) || %i[create update destroy]
|
19
|
-
|
20
|
-
|
19
|
+
actions.each do |action|
|
20
|
+
info = settings.merge(klass: name, action: action, attrs: attrs)
|
21
|
+
PubSubModelSync::Config.publishers << info
|
22
|
+
ps_register_callback(action.to_sym, info)
|
23
|
+
end
|
21
24
|
end
|
22
25
|
|
23
|
-
def
|
24
|
-
|
26
|
+
def ps_publisher_info(action = :create)
|
27
|
+
PubSubModelSync::Config.publishers.select do |listener|
|
28
|
+
listener[:klass] == name && listener[:action] == action
|
29
|
+
end.last
|
25
30
|
end
|
26
31
|
|
27
32
|
def ps_class_publish(data, action:, as_klass: nil)
|
28
33
|
as_klass = (as_klass || name).to_s
|
29
|
-
|
34
|
+
ps_publisher_service.publish_data(as_klass, data, action.to_sym)
|
30
35
|
end
|
31
36
|
|
32
|
-
def
|
37
|
+
def ps_publisher_service
|
33
38
|
PubSubModelSync::Publisher.new
|
34
39
|
end
|
35
40
|
|
36
41
|
private
|
37
42
|
|
38
|
-
def
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
publisher.publish_model(model, action.to_sym)
|
44
|
-
end
|
43
|
+
def ps_register_callback(action, info)
|
44
|
+
after_commit(on: action) do |model|
|
45
|
+
unless model.ps_skip_for?(action)
|
46
|
+
service = model.class.ps_publisher_service
|
47
|
+
service.publish_model(model, action.to_sym, info)
|
45
48
|
end
|
46
49
|
end
|
47
50
|
end
|
@@ -9,31 +9,34 @@ module PubSubModelSync
|
|
9
9
|
module ClassMethods
|
10
10
|
# @param settings (Hash): { as_klass: nil, actions: nil, id: nil }
|
11
11
|
def ps_subscribe(attrs, settings = {})
|
12
|
-
|
12
|
+
as_klass = (settings[:as_klass] || name).to_s
|
13
13
|
actions = settings.delete(:actions) || %i[create update destroy]
|
14
|
-
|
14
|
+
settings = settings.merge(attrs: attrs)
|
15
15
|
actions.each do |action|
|
16
|
-
add_ps_subscriber(
|
16
|
+
add_ps_subscriber(as_klass, action, action, false, settings)
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
20
20
|
def ps_class_subscribe(action, as_action: nil, as_klass: nil)
|
21
|
-
add_ps_subscriber(as_klass, action, as_action, true)
|
21
|
+
add_ps_subscriber(as_klass, action, as_action, true, {})
|
22
22
|
end
|
23
23
|
|
24
|
-
def
|
25
|
-
|
24
|
+
def ps_subscriber(action = :create)
|
25
|
+
PubSubModelSync::Config.listeners.select do |listener|
|
26
|
+
listener[:klass] == name && listener[:action] == action
|
27
|
+
end.last
|
26
28
|
end
|
27
29
|
|
28
30
|
private
|
29
31
|
|
30
|
-
def add_ps_subscriber(as_klass, action, as_action, direct_mode)
|
32
|
+
def add_ps_subscriber(as_klass, action, as_action, direct_mode, settings)
|
31
33
|
listener = {
|
32
34
|
klass: name,
|
33
35
|
as_klass: (as_klass || name).to_s,
|
34
36
|
action: action.to_sym,
|
35
37
|
as_action: (as_action || action).to_sym,
|
36
|
-
direct_mode: direct_mode
|
38
|
+
direct_mode: direct_mode,
|
39
|
+
settings: settings
|
37
40
|
}
|
38
41
|
PubSubModelSync::Config.listeners << listener
|
39
42
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pub_sub_model_sync
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Owen
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-03-
|
11
|
+
date: 2020-03-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|