From b510a7b11c1ad20297edae275207c2f66aa6fdb8 Mon Sep 17 00:00:00 2001 From: owen2345 Date: Wed, 9 Jun 2021 05:21:03 -0400 Subject: [PATCH] feat: rename publisher.rb into payload_builder chore: update readme --- README.md | 33 +++++++++---------- lib/pub_sub_model_sync.rb | 2 +- lib/pub_sub_model_sync/message_publisher.rb | 6 ++-- .../{publisher.rb => payload_builder.rb} | 4 +-- lib/pub_sub_model_sync/publisher_concern.rb | 2 +- ...lisher_spec.rb => payload_builder_spec.rb} | 4 +-- 6 files changed, 24 insertions(+), 27 deletions(-) rename lib/pub_sub_model_sync/{publisher.rb => payload_builder.rb} (96%) rename spec/{publisher_spec.rb => payload_builder_spec.rb} (96%) diff --git a/README.md b/README.md index bb66006..5610d79 100644 --- a/README.md +++ b/README.md @@ -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' @@ -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 }) @@ -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), @@ -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 diff --git a/lib/pub_sub_model_sync.rb b/lib/pub_sub_model_sync.rb index 794e320..8f6c30b 100644 --- a/lib/pub_sub_model_sync.rb +++ b/lib/pub_sub_model_sync.rb @@ -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' diff --git a/lib/pub_sub_model_sync/message_publisher.rb b/lib/pub_sub_model_sync/message_publisher.rb index 3493850..ea0eca0 100644 --- a/lib/pub_sub_model_sync/message_publisher.rb +++ b/lib/pub_sub_model_sync/message_publisher.rb @@ -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 diff --git a/lib/pub_sub_model_sync/publisher.rb b/lib/pub_sub_model_sync/payload_builder.rb similarity index 96% rename from lib/pub_sub_model_sync/publisher.rb rename to lib/pub_sub_model_sync/payload_builder.rb index 64d5f91..10952c1 100644 --- a/lib/pub_sub_model_sync/publisher.rb +++ b/lib/pub_sub_model_sync/payload_builder.rb @@ -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) @@ -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) diff --git a/lib/pub_sub_model_sync/publisher_concern.rb b/lib/pub_sub_model_sync/publisher_concern.rb index db6027e..8440b7a 100644 --- a/lib/pub_sub_model_sync/publisher_concern.rb +++ b/lib/pub_sub_model_sync/publisher_concern.rb @@ -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 diff --git a/spec/publisher_spec.rb b/spec/payload_builder_spec.rb similarity index 96% rename from spec/publisher_spec.rb rename to spec/payload_builder_spec.rb index 0468726..a8f9098 100644 --- a/spec/publisher_spec.rb +++ b/spec/payload_builder_spec.rb @@ -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 } @@ -79,6 +79,6 @@ private def payload_for(*args) - PubSubModelSync::Publisher.new(model, *args).payload + PubSubModelSync::PayloadBuilder.new(model, *args).call end end