Sha256: 4fde4e10e6c697179a2f44d12c6c93d132ea87d30ce1cbd7789631dcdb83e690

Contents?: true

Size: 1.41 KB

Versions: 1

Compression:

Stored size: 1.41 KB

Contents

module SyncMachine
  class EnsurePublication
    # Manage logic around whether the document was previously published at all,
    # and maintaining history for future jobs.
    class PublicationHistory
      def initialize(subject_id:, sync_module:)
        @subject_id = subject_id
        @sync_module = sync_module
      end

      def last_publish_equals?(payload_body)
        return false unless previous_payload
        HashWithIndifferentAccess.new(previous_payload.body) ==
          HashWithIndifferentAccess.new(payload_body)
      end

      def last_job_finished_at
        previous_payload.try(:generated_at)
      end

      def record_generation_time
        previous_payload.update_attribute(:generated_at, Time.now)
      end

      def update(payload_body)
        if previous_payload
          previous_payload.update_attributes(
            body: payload_body, generated_at: Time.now
          )
        else
          create_payload_record(payload_body)
        end
      end

      private

      def create_payload_record(payload_body)
        payload_class.create!(
          body: payload_body,
          generated_at: Time.now,
          subject_id: @subject_id.to_s
        )
      end

      def payload_class
        @payload_class ||= @sync_module.const_get('Payload')
      end

      def previous_payload
        @previous_payload ||= payload_class.where(subject_id: @subject_id).first
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sync_machine-0.1.0 lib/sync_machine/ensure_publication/publication_history.rb