pub_sub_model_sync 0.1.2 → 0.1.3

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
  SHA256:
3
- metadata.gz: 2474d763e9b95cd2ddf83284ed095af1e1c8d53fe4ffc89fbe7249e8c198d38b
4
- data.tar.gz: 87ab9ca1c4167ebf314c1bdbd48bd7216e7e1036acbbb2a6c93f82ddd7bb80e6
3
+ metadata.gz: 1134a868bdf5d405f2cd05dbca21806c6cc3c8240ae5951f8a26146f2f98917a
4
+ data.tar.gz: 3d2c96dfd3155827f178ef824074f84a5e1be04408cbaa2948c6536ea6b34e48
5
5
  SHA512:
6
- metadata.gz: 7a9ecb33cb7e85a76d797de1e390cf2a6a1086c245512695e46610cc18c1fa9273c821002177d30bc78ffe4159239733f287a665b69ded4f0d4490486072ff97
7
- data.tar.gz: be9b8bf52ae416e9bf2667c3a6aa18cbbdc30aad265d71f00f1b90594a1f09d5a7498843e6f5361dd1562f151f0a7d73b7166bc4351fe02992a4dd1080ec587c
6
+ metadata.gz: fb61cce4b0170bb86137a7f51452c703804e2eb4523ec5729eb8a77fabd768e215a9029f200795a2662124621c2b5aa5d77fdfbdecdcafb1d380e3524fa8eb34
7
+ data.tar.gz: d6a6463def886802bd5a353468adfaa12d1a5d94885d978e85c65f26b03b4db1a05586a8dd051c780782f7dcdb2f57770a6b482c663df880cd40d2f75acd3515
@@ -4,7 +4,7 @@ language: ruby
4
4
  cache: bundler
5
5
  rvm:
6
6
  - 2.3.1
7
- before_install: gem install bundler -v 2.0.2
7
+ before_install: gem install bundler
8
8
 
9
9
  script:
10
10
  - bundle install
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- pub_sub_model_sync (0.1.2)
4
+ pub_sub_model_sync (0.1.3)
5
5
  activesupport
6
6
  rails
7
7
 
data/README.md CHANGED
@@ -57,14 +57,14 @@ And then execute: $ bundle install
57
57
  # attributes: name email age
58
58
  class User < ActiveRecord::Base
59
59
  include PubSubModelSync::PublisherConcern
60
- ps_msync_publish(%i[name email])
60
+ ps_publish(%i[name email])
61
61
  end
62
62
 
63
63
  # App 2
64
64
  class User < ActiveRecord::Base
65
65
  include PubSubModelSync::SubscriberConcern
66
- ps_msync_subscribe(%i[name])
67
- ps_msync_class_subscribe(:greeting)
66
+ ps_subscribe(%i[name])
67
+ ps_class_subscribe(:greeting)
68
68
 
69
69
  def self.greeting(data)
70
70
  puts 'Class message called'
@@ -73,7 +73,7 @@ end
73
73
 
74
74
  # Samples
75
75
  User.create(name: 'test user') # Review your App 2 to see the created user (only name will be saved)
76
- User.ps_msync_class_publish({ msg: 'Hello' }, action: :greeting) # User.greeting method (Class method) will be called in App2
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
@@ -82,9 +82,9 @@ User.ps_msync_class_publish({ msg: 'Hello' }, action: :greeting) # User.greeting
82
82
  class User < ActiveRecord::Base
83
83
  self.table_name = 'publisher_users'
84
84
  include PubSubModelSync::PublisherConcern
85
- ps_msync_publish(%i[name], actions: %i[update], as_klass: 'Client', id: :client_id)
85
+ ps_publish(%i[name], actions: %i[update], as_klass: 'Client', id: :client_id)
86
86
 
87
- def ps_msync_skip_for?(_action)
87
+ def ps_skip_for?(_action)
88
88
  false # here logic with action to skip push message
89
89
  end
90
90
  end
@@ -93,8 +93,8 @@ end
93
93
  class User < ActiveRecord::Base
94
94
  self.table_name = 'subscriber_users'
95
95
  include PubSubModelSync::SubscriberConcern
96
- ps_msync_subscribe(%i[name], actions: %i[update], as_klass: 'Client', id: :custom_id)
97
- ps_msync_class_subscribe(:greeting, as_action: :custom_greeting, as_klass: 'CustomUser')
96
+ ps_subscribe(%i[name], actions: %i[update], as_klass: 'Client', id: :custom_id)
97
+ ps_class_subscribe(:greeting, as_action: :custom_greeting, as_klass: 'CustomUser')
98
98
 
99
99
  def self.greeting(data)
100
100
  puts 'Class message called through custom_greeting'
@@ -147,7 +147,7 @@ end
147
147
  publisher = PubSubModelSync::Publisher
148
148
  data = { name: 'hello'}
149
149
  action = :create
150
- User.ps_msync_class_publish(data, action: action)
150
+ User.ps_class_publish(data, action: action)
151
151
  user = User.create(name: 'name', email: 'email')
152
152
  expect_any_instance_of(publisher).to receive(:publish_model).with(user, :create, anything)
153
153
  end
@@ -156,16 +156,16 @@ end
156
156
  publisher = PubSubModelSync::Publisher
157
157
  data = {msg: 'hello'}
158
158
  action = :greeting
159
- User.ps_msync_class_publish(data, action: action)
159
+ User.ps_class_publish(data, action: action)
160
160
  expect_any_instance_of(publisher).to receive(:publish_data).with('User', data, action)
161
161
  end
162
162
  ```
163
163
 
164
164
  There are two special methods to extract crud configuration settings (attrs, id, ...):
165
165
 
166
- Subscribers: ```User.ps_msync_subscriber_settings```
166
+ Subscribers: ```User.ps_subscriber_settings```
167
167
 
168
- Publishers: ```User.ps_msync_publisher_settings```
168
+ Publishers: ```User.ps_publisher_settings```
169
169
 
170
170
  Note: Inspect all configured listeners with:
171
171
  ``` PubSubModelSync::Config.listeners ```
@@ -14,7 +14,11 @@ module PubSubModelSync
14
14
 
15
15
  def self.log(msg, kind = :info)
16
16
  msg = "PS_MSYNC ==> #{msg}"
17
- logger ? logger.send(kind, msg) : puts(msg)
17
+ if logger == :raise_error
18
+ kind == :error ? raise(msg) : puts(msg)
19
+ else
20
+ logger ? logger.send(kind, msg) : puts(msg)
21
+ end
18
22
  end
19
23
  end
20
24
  end
@@ -76,7 +76,7 @@ module PubSubModelSync
76
76
 
77
77
  def listener_add_crud_settings(listener)
78
78
  model_class = listener[:klass].constantize
79
- listener[:settings] = model_class.ps_msync_subscriber_settings
79
+ listener[:settings] = model_class.ps_subscriber_settings
80
80
  end
81
81
 
82
82
  def log(message, kind = :info)
@@ -14,7 +14,7 @@ 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.ps_msync_publisher_settings
17
+ settings ||= model.class.ps_publisher_settings
18
18
  attributes = build_model_attrs(model, action, settings)
19
19
  data = {}
20
20
  if action != 'destroy'
@@ -7,39 +7,39 @@ module PubSubModelSync
7
7
  end
8
8
 
9
9
  # Permit to skip a publish callback
10
- def ps_msync_skip_for?(_action)
10
+ def ps_skip_for?(_action)
11
11
  false
12
12
  end
13
13
 
14
14
  module ClassMethods
15
15
  # Permit to publish crud actions (:create, :update, :destroy)
16
16
  # @param settings (Hash): { actions: nil, as_klass: nil, id: nil }
17
- def ps_msync_publish(attrs, settings = {})
17
+ def ps_publish(attrs, settings = {})
18
18
  actions = settings.delete(:actions) || %i[create update destroy]
19
- @ps_msync_publisher_settings = settings.merge(attrs: attrs)
20
- ps_msync_register_callbacks(actions)
19
+ @ps_publisher_settings = settings.merge(attrs: attrs)
20
+ ps_register_callbacks(actions)
21
21
  end
22
22
 
23
- def ps_msync_publisher_settings
24
- @ps_msync_publisher_settings
23
+ def ps_publisher_settings
24
+ @ps_publisher_settings
25
25
  end
26
26
 
27
- def ps_msync_class_publish(data, action:, as_klass: nil)
27
+ def ps_class_publish(data, action:, as_klass: nil)
28
28
  as_klass = (as_klass || name).to_s
29
- ps_msync_publisher.publish_data(as_klass, data, action.to_sym)
29
+ ps_publisher.publish_data(as_klass, data, action.to_sym)
30
30
  end
31
31
 
32
- def ps_msync_publisher
32
+ def ps_publisher
33
33
  PubSubModelSync::Publisher.new
34
34
  end
35
35
 
36
36
  private
37
37
 
38
- def ps_msync_register_callbacks(actions)
38
+ def ps_register_callbacks(actions)
39
39
  actions.each do |action|
40
40
  after_commit(on: action) do |model|
41
- unless model.ps_msync_skip_for?(action)
42
- publisher = model.class.ps_msync_publisher
41
+ unless model.ps_skip_for?(action)
42
+ publisher = model.class.ps_publisher
43
43
  publisher.publish_model(model, action.to_sym)
44
44
  end
45
45
  end
@@ -54,13 +54,13 @@ module PubSubModelSync
54
54
  args = [data, attrs[:klass], attrs[:action], attrs]
55
55
  PubSubModelSync::MessageProcessor.new(*args).process
56
56
  rescue => e
57
- log("Error processing message: #{[received_message, e.message]}")
57
+ log("Error processing message: #{[received_message, e.message]}", :error)
58
58
  ensure
59
59
  received_message.acknowledge!
60
60
  end
61
61
 
62
- def log(msg)
63
- config.log("Google Service ==> #{msg}")
62
+ def log(msg, kind = :info)
63
+ config.log("Google Service ==> #{msg}", kind)
64
64
  end
65
65
  end
66
66
  end
@@ -24,7 +24,7 @@ module PubSubModelSync
24
24
  rescue PubSubModelSync::Runner::ShutDown
25
25
  raise
26
26
  rescue => e
27
- log("Error listening message: #{[e.message, e.backtrace]}")
27
+ log("Error listening message: #{[e.message, e.backtrace]}", :error)
28
28
  end
29
29
 
30
30
  def publish(data, attributes)
@@ -33,7 +33,8 @@ module PubSubModelSync
33
33
  payload = { data: data, attributes: attributes }
34
34
  topic.publish(payload.to_json, routing_key: queue.name, type: SERVICE_KEY)
35
35
  rescue => e
36
- log("Error publishing: #{[data, attributes, e.message, e.backtrace]}")
36
+ info = [data, attributes, e.message, e.backtrace]
37
+ log("Error publishing: #{info}", :error)
37
38
  end
38
39
 
39
40
  def stop
@@ -51,7 +52,7 @@ module PubSubModelSync
51
52
  PubSubModelSync::MessageProcessor.new(*args).process
52
53
  rescue => e
53
54
  error = [payload, e.message, e.backtrace]
54
- log("Error processing message: #{error}")
55
+ log("Error processing message: #{error}", :error)
55
56
  end
56
57
 
57
58
  def parse_message_payload(payload)
@@ -74,8 +75,8 @@ module PubSubModelSync
74
75
  queue.bind(topic, routing_key: queue.name)
75
76
  end
76
77
 
77
- def log(msg)
78
- config.log("Rabbit Service ==> #{msg}")
78
+ def log(msg, kind = :info)
79
+ config.log("Rabbit Service ==> #{msg}", kind)
79
80
  end
80
81
  end
81
82
  end
@@ -8,26 +8,26 @@ module PubSubModelSync
8
8
 
9
9
  module ClassMethods
10
10
  # @param settings (Hash): { as_klass: nil, actions: nil, id: nil }
11
- def ps_msync_subscribe(attrs, settings = {})
11
+ def ps_subscribe(attrs, settings = {})
12
12
  settings[:as_klass] = (settings[:as_klass] || name).to_s
13
13
  actions = settings.delete(:actions) || %i[create update destroy]
14
- @ps_msync_subscriber_settings = { attrs: attrs }.merge(settings)
14
+ @ps_subscriber_settings = { attrs: attrs }.merge(settings)
15
15
  actions.each do |action|
16
- add_ps_msync_subscriber(settings[:as_klass], action, action, false)
16
+ add_ps_subscriber(settings[:as_klass], action, action, false)
17
17
  end
18
18
  end
19
19
 
20
- def ps_msync_class_subscribe(action, as_action: nil, as_klass: nil)
21
- add_ps_msync_subscriber(as_klass, action, as_action, true)
20
+ def ps_class_subscribe(action, as_action: nil, as_klass: nil)
21
+ add_ps_subscriber(as_klass, action, as_action, true)
22
22
  end
23
23
 
24
- def ps_msync_subscriber_settings
25
- @ps_msync_subscriber_settings || {}
24
+ def ps_subscriber_settings
25
+ @ps_subscriber_settings || {}
26
26
  end
27
27
 
28
28
  private
29
29
 
30
- def add_ps_msync_subscriber(as_klass, action, as_action, direct_mode)
30
+ def add_ps_subscriber(as_klass, action, as_action, direct_mode)
31
31
  listener = {
32
32
  klass: name,
33
33
  as_klass: (as_klass || name).to_s,
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PubSubModelSync
4
- VERSION = '0.1.2'
4
+ VERSION = '0.1.3'
5
5
  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.2
4
+ version: 0.1.3
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-16 00:00:00.000000000 Z
11
+ date: 2020-03-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport