Skip to content

Commit

Permalink
feat: rename publisher.rb into payload_builder
Browse files Browse the repository at this point in the history
chore: update readme
  • Loading branch information
owen2345 committed Jun 9, 2021
1 parent 40ab3c5 commit b510a7b
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 27 deletions.
33 changes: 16 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ Any notification before delivering is transformed as a Payload for a better port

## **Testing with RSpec**
- Config: (spec/rails_helper.rb)
```ruby
```ruby
# when using google service
require 'pub_sub_model_sync/mock_google_service'
Expand All @@ -402,30 +402,29 @@ Any notification before delivering is transformed as a Payload for a better port
allow(Kafka).to receive(:new).and_return(kafka_mock)
end
#
# disable all models sync by default (reduces testing time)
config.before(:each) do
# **** disable payloads generation, sync callbacks to improve tests speed
allow(PubSubModelSync::MessagePublisher).to receive(:publish_data) # disable class level notif
allow(PubSubModelSync::MessagePublisher).to receive(:publish_model) # disable instance level notif
# **** when testing model syncs, it can be re enabled by:
# before do
# allow(PubSubModelSync::MessagePublisher).to receive(:publish_data).and_call_original
# allow(PubSubModelSync::MessagePublisher).to receive(:publish_model).and_call_original
# end
end
```
# enable all models sync only for tests that includes 'sync: true'
config.before(:each, sync: true) do
allow(PubSubModelSync::MessagePublisher).to receive(:publish_data).and_call_original
allow(PubSubModelSync::MessagePublisher).to receive(:publish_model).and_call_original
end
```
- Examples:
```ruby
```ruby
# Subscriber
it 'receive model notification' do
it 'receive model notification', sync: true do
data = { name: 'name', id: 999 }
payload = PubSubModelSync::Payload.new(data, { klass: 'User', action: :create })
payload.process!
expect(User.find(data[:id])).not_to be_nil
end
it 'receive class notification' do
it 'receive class notification', sync: true do
data = { msg: 'hello' }
action = :greeting
payload = PubSubModelSync::Payload.new(data, { klass: 'User', action: action, mode: :klass })
Expand All @@ -434,13 +433,13 @@ Any notification before delivering is transformed as a Payload for a better port
end
# Publisher
it 'publishes model notification' do
it 'publishes model notification', sync: true do
publisher = PubSubModelSync::MessagePublisher
expect(publisher).to receive(:publish_model).with(be_a(User), :create, anything)
User.create(name: 'name', email: 'email')
end
it 'publishes the correct values in the payload' do
it 'publishes the correct values in the payload', sync: true do
publisher = PubSubModelSync::MessagePublisher
exp_data = have_attributes(data: hash_including(email: 'email'),
info: hash_including(klass: 'User', action: :create),
Expand All @@ -449,14 +448,14 @@ Any notification before delivering is transformed as a Payload for a better port
User.create(name: 'name', email: 'email')
end
it 'publishes class notification' do
it 'publishes class notification', sync: true do
publisher = PubSubModelSync::MessagePublisher
user = User.create(name: 'name', email: 'email')
data = { msg: 'hello' }
user.ps_class_publish(data, action: :greeting)
expect(publisher).to receive(:publish_data).with('User', data, :greeting, anything)
end
```
```

## **Extra configurations**
```ruby
Expand Down
2 changes: 1 addition & 1 deletion lib/pub_sub_model_sync.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
require 'pub_sub_model_sync/message_processor'
require 'pub_sub_model_sync/run_subscriber'

require 'pub_sub_model_sync/publisher'
require 'pub_sub_model_sync/payload_builder'
require 'pub_sub_model_sync/subscriber'

require 'pub_sub_model_sync/service_base'
Expand Down
6 changes: 2 additions & 4 deletions lib/pub_sub_model_sync/message_publisher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,9 @@ def publish_data(klass, data, action, headers: {})

# @param model (ActiveRecord::Base)
# @param action (Symbol: @see PublishConcern::ps_publish)
# @param settings (Hash: @see Publisher.new.settings)
# @param settings (Hash: @see PayloadBuilder.settings)
def publish_model(model, action, settings = {})
publisher = PubSubModelSync::Publisher.new(model, action, settings)
payload = publisher.payload

payload = PubSubModelSync::PayloadBuilder.new(model, action, settings).call
transaction(payload.headers[:ordering_key]) do # catch and group all :ps_before_publish syncs
publish(payload) { model.ps_after_publish(action, payload) } if ensure_model_publish(model, action, payload)
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

module PubSubModelSync
class Publisher < PubSubModelSync::Base
class PayloadBuilder < PubSubModelSync::Base
attr_accessor :model, :action, :data, :mapping, :headers, :as_klass

# @param model (ActiveRecord::Base)
Expand All @@ -17,7 +17,7 @@ def initialize(model, action, settings = {})
end

# @return (Payload)
def payload
def call
values = compute_value(data)
values = mapping_data.merge(values)
PubSubModelSync::Payload.new(values, settings_data, headers_data)
Expand Down
2 changes: 1 addition & 1 deletion lib/pub_sub_model_sync/publisher_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def ps_crud_define_commit_action(action, callback)
# Initialize calls to start and end pub_sub transactions and deliver all them in the same order
def ps_init_transaction_callbacks
start_transaction = lambda do
key = id ? PubSubModelSync::Publisher.ordering_key_for(self) : nil
key = id ? PubSubModelSync::PayloadBuilder.ordering_key_for(self) : nil
@ps_transaction = PubSubModelSync::MessagePublisher.init_transaction(key)
end
after_create start_transaction, prepend: true # wait for ID
Expand Down
4 changes: 2 additions & 2 deletions spec/publisher_spec.rb → spec/payload_builder_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

RSpec.describe PubSubModelSync::Publisher do
RSpec.describe PubSubModelSync::PayloadBuilder do
let(:model) { PublisherUser.new(id: 1, name: 'name', email: 'email', age: 10) }
let(:action) { :update }
let(:klass_name) { model.class.name }
Expand Down Expand Up @@ -79,6 +79,6 @@
private

def payload_for(*args)
PubSubModelSync::Publisher.new(model, *args).payload
PubSubModelSync::PayloadBuilder.new(model, *args).call
end
end

0 comments on commit b510a7b

Please sign in to comment.