Sha256: a64ef3d155a9c165500b68027d0f3f8508b749ec17412945ef9e1d97b7291ad7

Contents?: true

Size: 1.37 KB

Versions: 11

Compression:

Stored size: 1.37 KB

Contents

# frozen_string_literal: true

class TableSync::Publishing::Single
  include Tainbox
  include Memery

  attribute :object_class
  attribute :original_attributes
  attribute :debounce_time

  attribute :event, Symbol, default: :update

  # expect job to have perform_at method
  # debounce destroyed event
  # because otherwise update event could be sent after destroy
  def publish_later
    return if debounce.skip?

    job.perform_at(job_attributes)

    debounce.cache_next_sync_time
  end

  def publish_now
    message.publish unless message.empty?
  end

  memoize def message
    TableSync::Publishing::Message::Single.new(attributes)
  end

  memoize def debounce
    TableSync::Publishing::Helpers::Debounce.new(
      object_class: object_class,
      needle: message.object.needle,
      debounce_time: debounce_time,
      event: event,
    )
  end

  private

  # JOB

  def job
    if TableSync.single_publishing_job_class_callable
      TableSync.single_publishing_job_class_callable.call
    else
      raise TableSync::NoCallableError.new("single_publishing_job_class")
    end
  end

  def job_attributes
    attributes.merge(
      original_attributes: serialized_original_attributes,
      perform_at: debounce.next_sync_time,
    )
  end

  def serialized_original_attributes
    TableSync::Publishing::Helpers::Attributes
      .new(original_attributes)
      .serialize
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

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