Sha256: f98c750f12121b50d38e594920f1876398b286a2ee1f1003e8504703b23eaaa9

Contents?: true

Size: 1.39 KB

Versions: 11

Compression:

Stored size: 1.39 KB

Contents

# frozen_string_literal: true

module TableSync::Publishing::Message
  class Base
    include Tainbox

    attr_reader :objects

    attribute :object_class
    attribute :original_attributes
    attribute :event

    def initialize(params)
      super(params)

      @objects = find_or_init_objects

      raise TableSync::NoObjectsForSyncError if objects.empty? && TableSync.raise_on_empty_message
    end

    def publish
      return if original_attributes.blank?

      Rabbit.publish(message_params)

      notify!
    end

    def empty?
      objects.empty?
    end

    def find_or_init_objects
      TableSync::Publishing::Helpers::Objects.new(
        object_class: object_class, original_attributes: original_attributes, event: event,
      ).construct_list
    end

    # MESSAGE PARAMS

    def message_params
      params.merge(data: data)
    end

    def data
      TableSync::Publishing::Data::Objects.new(
        objects: objects, event: event,
      ).construct
    end

    # :nocov:
    def params
      raise NotImplementedError
    end
    # :nocov:

    # NOTIFY

    def notify!
      TableSync::Instrument.notify(
        table: model_naming.table,
        schema: model_naming.schema,
        event: event,
        direction: :publish,
        count: objects.count,
      )
    end

    def model_naming
      TableSync.publishing_adapter.model_naming(objects.first.object_class)
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
table_sync-6.5.1 lib/table_sync/publishing/message/base.rb
table_sync-6.5.0 lib/table_sync/publishing/message/base.rb
table_sync-6.4.2 lib/table_sync/publishing/message/base.rb
table_sync-6.4.1 lib/table_sync/publishing/message/base.rb
table_sync-6.4.0 lib/table_sync/publishing/message/base.rb
table_sync-6.3.0 lib/table_sync/publishing/message/base.rb
table_sync-6.1.0 lib/table_sync/publishing/message/base.rb
table_sync-6.0.4 lib/table_sync/publishing/message/base.rb
table_sync-6.0.3 lib/table_sync/publishing/message/base.rb
table_sync-6.0.2 lib/table_sync/publishing/message/base.rb
table_sync-6.0 lib/table_sync/publishing/message/base.rb