pub_sub_model_sync 0.3.1 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +7 -0
- data/Gemfile.lock +1 -1
- data/README.md +13 -13
- data/lib/pub_sub_model_sync.rb +4 -1
- data/lib/pub_sub_model_sync/config.rb +1 -1
- data/lib/pub_sub_model_sync/message_processor.rb +15 -67
- data/lib/pub_sub_model_sync/message_publisher.rb +33 -0
- data/lib/pub_sub_model_sync/publisher.rb +13 -36
- data/lib/pub_sub_model_sync/publisher_concern.rb +20 -20
- data/lib/pub_sub_model_sync/service_base.rb +1 -1
- data/lib/pub_sub_model_sync/service_google.rb +3 -1
- data/lib/pub_sub_model_sync/subscriber.rb +65 -0
- data/lib/pub_sub_model_sync/subscriber_concern.rb +15 -18
- data/lib/pub_sub_model_sync/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 733ca5e3413a098031be8a2ca5d0c3df9bf972ed8cdbf8954a1968c517ac81f4
|
4
|
+
data.tar.gz: a4c419efb5446e8a8632942936589b1b0a4bad2ab913a9ddd34cffb2bfdf8f2a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3054010e2bad46d8d2377a01757b0f52abc31f27832ddc150b12f3fb5d05f50a9aa87aa1bcafff7237918ecdc3d4cc637033dec76d32d7991d69c36d5e949ea8
|
7
|
+
data.tar.gz: 734be5a88e56fa6f508f1c37acdada68cf3b53db5b2d5b3d612616c27a63f4d46c305d0d2148d656c970d0a6cc9e25ec6840ead4c4a74b1c4f39b777a791e2cc
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,12 @@
|
|
1
1
|
# Change Log
|
2
2
|
|
3
|
+
# 0.4.0 (May 06, 2020)
|
4
|
+
- rename as_klass to from_klass and as_action to from_action for subscribers
|
5
|
+
- refactor subscribers to be independent
|
6
|
+
- refactor message_publisher to use publisher
|
7
|
+
- rename publisher into message_publisher
|
8
|
+
- reformat publisher to reuse connector
|
9
|
+
|
3
10
|
# 0.3.1 (May 05, 2020)
|
4
11
|
- improve rabbit service to use sleep instead of block ("Block is not recommended for production")
|
5
12
|
- improve message ID
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -86,7 +86,7 @@ User.create(name: 'test user', email: 'sample@gmail.com') # Review your App 2 to
|
|
86
86
|
User.new(name: 'test user').ps_perform_sync(:create) # similar to above to perform sync on demand
|
87
87
|
|
88
88
|
User.ps_class_publish({ msg: 'Hello' }, action: :greeting) # User.greeting method (Class method) will be called in App2
|
89
|
-
PubSubModelSync::
|
89
|
+
PubSubModelSync::MessagePublisher.publish_data(User, { msg: 'Hello' }, :greeting) # similar to above when not included publisher concern
|
90
90
|
```
|
91
91
|
|
92
92
|
## Advanced Example
|
@@ -110,8 +110,8 @@ end
|
|
110
110
|
class User < ActiveRecord::Base
|
111
111
|
self.table_name = 'subscriber_users'
|
112
112
|
include PubSubModelSync::SubscriberConcern
|
113
|
-
ps_subscribe(%i[name], actions: %i[update],
|
114
|
-
ps_class_subscribe(:greeting,
|
113
|
+
ps_subscribe(%i[name], actions: %i[update], from_klass: 'Client', id: %i[client_id email])
|
114
|
+
ps_class_subscribe(:greeting, from_action: :custom_greeting, from_klass: 'CustomUser')
|
115
115
|
alias_attribute :full_name, :name
|
116
116
|
|
117
117
|
def self.greeting(data)
|
@@ -139,14 +139,14 @@ end
|
|
139
139
|
## API
|
140
140
|
### Subscribers
|
141
141
|
- Permit to configure class level listeners
|
142
|
-
```ps_class_subscribe(action_name,
|
143
|
-
*
|
144
|
-
*
|
142
|
+
```ps_class_subscribe(action_name, from_action: nil, from_klass: nil)```
|
143
|
+
* from_action: (Optional) Source method name
|
144
|
+
* from_klass: (Optional) Source class name
|
145
145
|
|
146
146
|
- Permit to configure instance level listeners (CRUD)
|
147
|
-
```ps_subscribe(attrs,
|
147
|
+
```ps_subscribe(attrs, from_klass: nil, actions: nil, id: nil)```
|
148
148
|
* attrs: (Array/Required) Array of all attributes to be synced
|
149
|
-
*
|
149
|
+
* from_klass: (String/Optional) Source class name (Instead of the model class name, will use this value)
|
150
150
|
* actions: (Array/Optional, default: create/update/destroy) permit to customize action names
|
151
151
|
* id: (Sym|Array/Optional, default: id) Attr identifier(s) to find the corresponding model
|
152
152
|
|
@@ -198,7 +198,7 @@ end
|
|
198
198
|
* as_klass: (optional, :string) Custom class name (Default current model name)
|
199
199
|
|
200
200
|
- Publish a class level notification (Same as above: on demand call)
|
201
|
-
```PubSubModelSync::
|
201
|
+
```PubSubModelSync::MessagePublisher.publish_data(Klass_name, data, action_name)```
|
202
202
|
* klass_name: (required, Class) same class name as defined in ps_class_subscribe(...)
|
203
203
|
* data: (required, :hash) message value to deliver
|
204
204
|
* action_name: (required, :sim) same action name as defined in ps_class_subscribe(...)
|
@@ -254,20 +254,20 @@ end
|
|
254
254
|
|
255
255
|
# Publisher
|
256
256
|
it 'publish model action' do
|
257
|
-
publisher = PubSubModelSync::
|
257
|
+
publisher = PubSubModelSync::MessagePublisher
|
258
258
|
data = { name: 'hello'}
|
259
259
|
action = :create
|
260
260
|
User.ps_class_publish(data, action: action)
|
261
261
|
user = User.create(name: 'name', email: 'email')
|
262
|
-
|
262
|
+
expect(publisher).to receive(:publish_model).with(user, :create, anything)
|
263
263
|
end
|
264
264
|
|
265
265
|
it 'publish class message' do
|
266
|
-
publisher = PubSubModelSync::
|
266
|
+
publisher = PubSubModelSync::MessagePublisher
|
267
267
|
data = {msg: 'hello'}
|
268
268
|
action = :greeting
|
269
269
|
User.ps_class_publish(data, action: action)
|
270
|
-
|
270
|
+
expect(publisher).to receive(:publish_data).with('User', data, action)
|
271
271
|
end
|
272
272
|
```
|
273
273
|
|
data/lib/pub_sub_model_sync.rb
CHANGED
@@ -6,12 +6,15 @@ require 'active_support'
|
|
6
6
|
require 'pub_sub_model_sync/railtie'
|
7
7
|
require 'pub_sub_model_sync/config'
|
8
8
|
require 'pub_sub_model_sync/subscriber_concern'
|
9
|
-
require 'pub_sub_model_sync/
|
9
|
+
require 'pub_sub_model_sync/message_publisher'
|
10
10
|
require 'pub_sub_model_sync/publisher_concern'
|
11
11
|
require 'pub_sub_model_sync/runner'
|
12
12
|
require 'pub_sub_model_sync/connector'
|
13
13
|
require 'pub_sub_model_sync/message_processor'
|
14
14
|
|
15
|
+
require 'pub_sub_model_sync/publisher'
|
16
|
+
require 'pub_sub_model_sync/subscriber'
|
17
|
+
|
15
18
|
require 'pub_sub_model_sync/service_base'
|
16
19
|
require 'pub_sub_model_sync/service_google'
|
17
20
|
require 'pub_sub_model_sync/service_rabbit'
|
@@ -2,91 +2,39 @@
|
|
2
2
|
|
3
3
|
module PubSubModelSync
|
4
4
|
class MessageProcessor
|
5
|
-
attr_accessor :data, :
|
5
|
+
attr_accessor :data, :klass, :action
|
6
6
|
|
7
7
|
# @param data (Hash): any hash value to deliver
|
8
8
|
def initialize(data, klass, action)
|
9
9
|
@data = data
|
10
|
-
@
|
11
|
-
@
|
10
|
+
@klass = klass
|
11
|
+
@action = action
|
12
12
|
end
|
13
13
|
|
14
14
|
def process
|
15
|
-
|
16
|
-
|
17
|
-
listeners = filter_listeners
|
18
|
-
return log 'Skipped: No listeners' unless listeners.any?
|
19
|
-
|
20
|
-
eval_message(listeners)
|
21
|
-
log 'processed message' unless @failed
|
15
|
+
subscribers = filter_subscribers
|
16
|
+
subscribers.each { |subscriber| run_subscriber(subscriber) }
|
22
17
|
end
|
23
18
|
|
24
19
|
private
|
25
20
|
|
26
|
-
def
|
27
|
-
|
28
|
-
|
29
|
-
call_class_listener(listener)
|
30
|
-
else
|
31
|
-
call_listener(listener)
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
def call_class_listener(listener)
|
37
|
-
model_class = listener[:klass].constantize
|
38
|
-
model_class.send(listener[:action], data)
|
21
|
+
def run_subscriber(subscriber)
|
22
|
+
subscriber.eval_message(data)
|
23
|
+
log "processed message with: #{[klass, action, data]}"
|
39
24
|
rescue => e
|
40
|
-
|
41
|
-
|
42
|
-
end
|
43
|
-
|
44
|
-
# support for: create, update, destroy
|
45
|
-
def call_listener(listener)
|
46
|
-
model = find_model(listener)
|
47
|
-
if settings[:action].to_sym == :destroy
|
48
|
-
model.destroy!
|
49
|
-
else
|
50
|
-
populate_model(model, listener)
|
51
|
-
model.save!
|
52
|
-
end
|
53
|
-
rescue => e
|
54
|
-
log("Error listener (#{listener}): #{e.message}", :error)
|
55
|
-
@failed = true
|
56
|
-
end
|
57
|
-
|
58
|
-
def find_model(listener)
|
59
|
-
model_class = listener[:klass].constantize
|
60
|
-
if model_class.respond_to?(:ps_find_model)
|
61
|
-
return model_class.ps_find_model(data, settings)
|
62
|
-
end
|
63
|
-
|
64
|
-
model_class.where(model_identifiers(listener)).first_or_initialize
|
65
|
-
end
|
66
|
-
|
67
|
-
def model_identifiers(listener)
|
68
|
-
identifiers = listener[:settings][:id]
|
69
|
-
identifiers = [identifiers] unless identifiers.is_a?(Array)
|
70
|
-
identifiers.map { |key| [key, data[key.to_sym]] }.to_h
|
71
|
-
end
|
72
|
-
|
73
|
-
def populate_model(model, listener)
|
74
|
-
values = data.slice(*listener[:settings][:attrs])
|
75
|
-
values.each do |attr, value|
|
76
|
-
model.send("#{attr}=", value)
|
77
|
-
end
|
25
|
+
info = [klass, action, data, e.message, e.backtrace]
|
26
|
+
log("error processing message: #{info}", :error)
|
78
27
|
end
|
79
28
|
|
80
|
-
def
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
listener[:as_action].to_s == settings[:action].to_s
|
29
|
+
def filter_subscribers
|
30
|
+
PubSubModelSync::Config.subscribers.select do |subscriber|
|
31
|
+
subscriber.settings[:from_klass].to_s == klass.to_s &&
|
32
|
+
subscriber.settings[:from_action].to_s == action.to_s
|
85
33
|
end
|
86
34
|
end
|
87
35
|
|
88
36
|
def log(message, kind = :info)
|
89
|
-
PubSubModelSync::Config.log
|
37
|
+
PubSubModelSync::Config.log message, kind
|
90
38
|
end
|
91
39
|
end
|
92
40
|
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module PubSubModelSync
|
4
|
+
class MessagePublisher
|
5
|
+
class << self
|
6
|
+
delegate :publish, to: :connector
|
7
|
+
|
8
|
+
def connector
|
9
|
+
@connector ||= PubSubModelSync::Connector.new
|
10
|
+
end
|
11
|
+
|
12
|
+
def publish_data(klass, data, action)
|
13
|
+
attrs = { klass: klass.to_s, action: action.to_sym }
|
14
|
+
publish(data, attrs)
|
15
|
+
end
|
16
|
+
|
17
|
+
# @param model: ActiveRecord model
|
18
|
+
# @param action: (Sym) Action name
|
19
|
+
# @param publisher: (Publisher, optional) Publisher to be used
|
20
|
+
def publish_model(model, action, publisher = nil)
|
21
|
+
return if model.ps_skip_sync?(action)
|
22
|
+
|
23
|
+
publisher ||= model.class.ps_publisher(action)
|
24
|
+
payload = publisher.payload(model, action)
|
25
|
+
res_before = model.ps_before_sync(action, payload[:data])
|
26
|
+
return if res_before == :cancel
|
27
|
+
|
28
|
+
publish(payload[:data], payload[:attrs])
|
29
|
+
model.ps_after_sync(action, payload[:data])
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -2,42 +2,24 @@
|
|
2
2
|
|
3
3
|
module PubSubModelSync
|
4
4
|
class Publisher
|
5
|
-
attr_accessor :
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
@
|
10
|
-
|
11
|
-
|
12
|
-
def publish_data(klass, data, action)
|
13
|
-
attributes = self.class.build_attrs(klass, action)
|
14
|
-
publish(data, attributes)
|
5
|
+
attr_accessor :attrs, :actions, :klass, :as_klass
|
6
|
+
def initialize(attrs, klass, actions = nil, as_klass = nil)
|
7
|
+
@attrs = attrs
|
8
|
+
@klass = klass
|
9
|
+
@actions = actions || %i[create update destroy]
|
10
|
+
@as_klass = as_klass || klass
|
15
11
|
end
|
16
12
|
|
17
|
-
|
18
|
-
|
19
|
-
return if model.ps_skip_sync?(action)
|
20
|
-
|
21
|
-
settings = model.class.ps_publisher(action).merge(custom_settings)
|
22
|
-
attributes = build_model_attrs(model, action, settings)
|
23
|
-
data = build_model_data(model, settings[:attrs])
|
24
|
-
res_before = model.ps_before_sync(action, data)
|
25
|
-
return if res_before == :cancel
|
26
|
-
|
27
|
-
publish(data.symbolize_keys, attributes)
|
28
|
-
model.ps_after_sync(action, data)
|
29
|
-
end
|
30
|
-
|
31
|
-
def self.build_attrs(klass, action)
|
32
|
-
{ klass: klass.to_s, action: action.to_sym }
|
13
|
+
def payload(model, action)
|
14
|
+
{ data: payload_data(model), attrs: payload_attrs(model, action) }
|
33
15
|
end
|
34
16
|
|
35
17
|
private
|
36
18
|
|
37
|
-
def
|
38
|
-
source_props =
|
19
|
+
def payload_data(model)
|
20
|
+
source_props = @attrs.map { |prop| prop.to_s.split(':').first }
|
39
21
|
data = model.as_json(only: source_props, methods: source_props)
|
40
|
-
aliased_props =
|
22
|
+
aliased_props = @attrs.select { |prop| prop.to_s.include?(':') }
|
41
23
|
aliased_props.each do |prop|
|
42
24
|
source, target = prop.to_s.split(':')
|
43
25
|
data[target] = data.delete(source)
|
@@ -45,13 +27,8 @@ module PubSubModelSync
|
|
45
27
|
data.symbolize_keys
|
46
28
|
end
|
47
29
|
|
48
|
-
def
|
49
|
-
|
50
|
-
self.class.build_attrs(as_klass, action)
|
51
|
-
end
|
52
|
-
|
53
|
-
def log(msg)
|
54
|
-
PubSubModelSync::Config.log(msg)
|
30
|
+
def payload_attrs(model, action)
|
31
|
+
{ klass: (as_klass || model.class.name).to_s, action: action.to_sym }
|
55
32
|
end
|
56
33
|
end
|
57
34
|
end
|
@@ -23,49 +23,49 @@ module PubSubModelSync
|
|
23
23
|
def ps_after_sync(_action, _data); end
|
24
24
|
|
25
25
|
# To perform sync on demand
|
26
|
-
# @param
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
26
|
+
# @param attrs (Array, optional): custom attrs to be used
|
27
|
+
# @param as_klass (Array, optional): custom klass name to be used
|
28
|
+
# @param publisher (Publisher, optional): custom publisher object
|
29
|
+
def ps_perform_sync(action = :create, attrs: nil, as_klass: nil,
|
30
|
+
publisher: nil)
|
31
|
+
publisher ||= self.class.ps_publisher(action).dup
|
32
|
+
publisher.attrs = attrs if attrs
|
33
|
+
publisher.as_klass = as_klass if as_klass
|
34
|
+
PubSubModelSync::MessagePublisher.publish_model(self, action, publisher)
|
31
35
|
end
|
32
36
|
|
33
37
|
module ClassMethods
|
34
38
|
# Permit to configure to publish crud actions (:create, :update, :destroy)
|
35
39
|
def ps_publish(attrs, actions: %i[create update destroy], as_klass: nil)
|
36
|
-
|
40
|
+
klass = PubSubModelSync::Publisher
|
41
|
+
publisher = klass.new(attrs, name, actions, as_klass)
|
42
|
+
PubSubModelSync::Config.publishers << publisher
|
37
43
|
actions.each do |action|
|
38
|
-
|
39
|
-
as_klass: as_klass }
|
40
|
-
PubSubModelSync::Config.publishers << info
|
41
|
-
ps_register_callback(action.to_sym, info)
|
44
|
+
ps_register_callback(action.to_sym, publisher)
|
42
45
|
end
|
43
46
|
end
|
44
47
|
|
45
48
|
# On demand class level publisher
|
46
49
|
def ps_class_publish(data, action:, as_klass: nil)
|
47
50
|
as_klass = (as_klass || name).to_s
|
48
|
-
|
51
|
+
klass = PubSubModelSync::MessagePublisher
|
52
|
+
klass.publish_data(as_klass, data, action.to_sym)
|
49
53
|
end
|
50
54
|
|
51
55
|
# Publisher info for specific action
|
52
56
|
def ps_publisher(action = :create)
|
53
|
-
PubSubModelSync::Config.publishers.find do |
|
54
|
-
|
57
|
+
PubSubModelSync::Config.publishers.find do |publisher|
|
58
|
+
publisher.klass == name && publisher.actions.include?(action)
|
55
59
|
end
|
56
60
|
end
|
57
61
|
|
58
|
-
def ps_publisher_service
|
59
|
-
PubSubModelSync::Publisher.new
|
60
|
-
end
|
61
|
-
|
62
62
|
private
|
63
63
|
|
64
|
-
def ps_register_callback(action,
|
64
|
+
def ps_register_callback(action, publisher)
|
65
65
|
after_commit(on: action) do |model|
|
66
66
|
unless model.ps_skip_callback?(action)
|
67
|
-
|
68
|
-
|
67
|
+
klass = PubSubModelSync::MessagePublisher
|
68
|
+
klass.publish_model(model, action.to_sym, publisher)
|
69
69
|
end
|
70
70
|
end
|
71
71
|
end
|
@@ -19,7 +19,7 @@ module PubSubModelSync
|
|
19
19
|
private
|
20
20
|
|
21
21
|
# @param payload (String JSON): '{"data":{}, "attributes":{..}}'
|
22
|
-
# refer: PubSubModelSync::
|
22
|
+
# refer: PubSubModelSync::MessagePublisher(.publish_model | .publish_data)
|
23
23
|
def perform_message(payload)
|
24
24
|
data, attrs = parse_message_payload(payload)
|
25
25
|
args = [data, attrs[:klass], attrs[:action]]
|
@@ -30,9 +30,11 @@ module PubSubModelSync
|
|
30
30
|
|
31
31
|
def publish(data, attributes)
|
32
32
|
log("Publishing message: #{[data, attributes]}")
|
33
|
-
|
34
33
|
payload = { data: data, attributes: attributes }.to_json
|
35
34
|
topic.publish(payload, { SERVICE_KEY => true })
|
35
|
+
rescue => e
|
36
|
+
info = [data, attributes, e.message, e.backtrace]
|
37
|
+
log("Error publishing: #{info}", :error)
|
36
38
|
end
|
37
39
|
|
38
40
|
def stop
|
@@ -0,0 +1,65 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module PubSubModelSync
|
4
|
+
class Subscriber
|
5
|
+
attr_accessor :klass, :action, :attrs, :settings
|
6
|
+
|
7
|
+
# @param settings: (Hash) { id: :id, direct_mode: false,
|
8
|
+
# from_klass: klass, from_action: action }
|
9
|
+
def initialize(klass, action, attrs: nil, settings: {})
|
10
|
+
def_settings = { id: :id, direct_mode: false,
|
11
|
+
from_klass: klass, from_action: action }
|
12
|
+
@klass = klass
|
13
|
+
@action = action
|
14
|
+
@attrs = attrs
|
15
|
+
@settings = def_settings.merge(settings)
|
16
|
+
end
|
17
|
+
|
18
|
+
def eval_message(message)
|
19
|
+
if settings[:direct_mode]
|
20
|
+
run_class_message(message)
|
21
|
+
else
|
22
|
+
run_model_message(message)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def run_class_message(message)
|
29
|
+
model_class = klass.constantize
|
30
|
+
model_class.send(action, message)
|
31
|
+
end
|
32
|
+
|
33
|
+
# support for: create, update, destroy
|
34
|
+
def run_model_message(message)
|
35
|
+
model = find_model(message)
|
36
|
+
if action == :destroy
|
37
|
+
model.destroy!
|
38
|
+
else
|
39
|
+
populate_model(model, message)
|
40
|
+
model.save!
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def find_model(message)
|
45
|
+
model_class = klass.constantize
|
46
|
+
if model_class.respond_to?(:ps_find_model)
|
47
|
+
return model_class.ps_find_model(message)
|
48
|
+
end
|
49
|
+
|
50
|
+
model_class.where(model_identifiers(message)).first_or_initialize
|
51
|
+
end
|
52
|
+
|
53
|
+
def model_identifiers(message)
|
54
|
+
identifiers = Array(settings[:id])
|
55
|
+
identifiers.map { |key| [key, message[key.to_sym]] }.to_h
|
56
|
+
end
|
57
|
+
|
58
|
+
def populate_model(model, message)
|
59
|
+
values = message.slice(*attrs)
|
60
|
+
values.each do |attr, value|
|
61
|
+
model.send("#{attr}=", value)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -7,37 +7,34 @@ module PubSubModelSync
|
|
7
7
|
end
|
8
8
|
|
9
9
|
module ClassMethods
|
10
|
-
def ps_subscribe(attrs,
|
10
|
+
def ps_subscribe(attrs, actions: nil, from_klass: name, id: :id)
|
11
|
+
settings = { id: id, from_klass: from_klass }
|
11
12
|
actions ||= %i[create update destroy]
|
12
|
-
settings = { attrs: attrs, id: id }
|
13
13
|
actions.each do |action|
|
14
|
-
add_ps_subscriber(
|
14
|
+
add_ps_subscriber(action, attrs, settings)
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
18
|
-
def ps_class_subscribe(action,
|
19
|
-
|
18
|
+
def ps_class_subscribe(action, from_action: nil, from_klass: nil)
|
19
|
+
settings = { direct_mode: true }
|
20
|
+
settings[:from_action] = from_action if from_action
|
21
|
+
settings[:from_klass] = from_klass if from_klass
|
22
|
+
add_ps_subscriber(action, nil, settings)
|
20
23
|
end
|
21
24
|
|
22
25
|
def ps_subscriber(action = :create)
|
23
|
-
PubSubModelSync::Config.
|
24
|
-
|
26
|
+
PubSubModelSync::Config.subscribers.find do |subscriber|
|
27
|
+
subscriber.klass == name && subscriber.action == action
|
25
28
|
end
|
26
29
|
end
|
27
30
|
|
28
31
|
private
|
29
32
|
|
30
|
-
# @param settings (Hash):
|
31
|
-
def add_ps_subscriber(
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
action: action.to_sym,
|
36
|
-
as_action: (as_action || action).to_sym,
|
37
|
-
direct_mode: direct_mode,
|
38
|
-
settings: settings
|
39
|
-
}
|
40
|
-
PubSubModelSync::Config.listeners.push(listener) && listener
|
33
|
+
# @param settings (Hash): refer to PubSubModelSync::Subscriber.settings
|
34
|
+
def add_ps_subscriber(action, attrs, settings = {})
|
35
|
+
klass = PubSubModelSync::Subscriber
|
36
|
+
subscriber = klass.new(name, action, attrs: attrs, settings: settings)
|
37
|
+
PubSubModelSync::Config.subscribers.push(subscriber) && subscriber
|
41
38
|
end
|
42
39
|
end
|
43
40
|
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.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Owen
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-05-
|
11
|
+
date: 2020-05-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -127,6 +127,7 @@ files:
|
|
127
127
|
- lib/pub_sub_model_sync/config.rb
|
128
128
|
- lib/pub_sub_model_sync/connector.rb
|
129
129
|
- lib/pub_sub_model_sync/message_processor.rb
|
130
|
+
- lib/pub_sub_model_sync/message_publisher.rb
|
130
131
|
- lib/pub_sub_model_sync/mock_google_service.rb
|
131
132
|
- lib/pub_sub_model_sync/mock_kafka_service.rb
|
132
133
|
- lib/pub_sub_model_sync/mock_rabbit_service.rb
|
@@ -138,6 +139,7 @@ files:
|
|
138
139
|
- lib/pub_sub_model_sync/service_google.rb
|
139
140
|
- lib/pub_sub_model_sync/service_kafka.rb
|
140
141
|
- lib/pub_sub_model_sync/service_rabbit.rb
|
142
|
+
- lib/pub_sub_model_sync/subscriber.rb
|
141
143
|
- lib/pub_sub_model_sync/subscriber_concern.rb
|
142
144
|
- lib/pub_sub_model_sync/tasks/worker.rake
|
143
145
|
- lib/pub_sub_model_sync/version.rb
|