Sha256: d06ad74144bf52f902f4fc5b135fbcccb75fd2837b5acecbbee3b52ea4f2db52

Contents?: true

Size: 1.88 KB

Versions: 8

Compression:

Stored size: 1.88 KB

Contents

class Journaled::Writer
  EVENT_METHOD_NAMES = %i(
    journaled_schema_name
    journaled_partition_key
    journaled_attributes
    journaled_app_name
  ).freeze

  def initialize(journaled_event:, priority: Journaled::JobPriority::EVENTUAL)
    raise "An enqueued event must respond to: #{EVENT_METHOD_NAMES.to_sentence}" unless respond_to_all?(journaled_event, EVENT_METHOD_NAMES)

    unless journaled_event.journaled_schema_name.present? &&
        journaled_event.journaled_partition_key.present? &&
        journaled_event.journaled_attributes.present?
      raise <<~ERROR
        An enqueued event must have a non-nil response to:
          #json_schema_name,
          #partition_key, and
          #journaled_attributes
      ERROR
    end

    @journaled_event = journaled_event
    @priority = priority
  end

  def journal!
    base_event_json_schema_validator.validate! serialized_event
    json_schema_validator.validate! serialized_event
    Delayed::Job.enqueue journaled_delivery, priority: priority
  end

  private

  attr_reader :journaled_event, :priority
  delegate :journaled_schema_name, :journaled_attributes, :journaled_partition_key, :journaled_app_name, to: :journaled_event

  def journaled_delivery
    @journaled_delivery ||= Journaled::Delivery.new(
      serialized_event: serialized_event,
      partition_key: journaled_partition_key,
      app_name: journaled_app_name
    )
  end

  def serialized_event
    @serialized_event ||= journaled_attributes.to_json
  end

  def json_schema_validator
    @json_schema_validator ||= Journaled::JsonSchemaModel::Validator.new(journaled_schema_name)
  end

  def base_event_json_schema_validator
    @base_event_json_schema_validator ||= Journaled::JsonSchemaModel::Validator.new('base_event')
  end

  def respond_to_all?(object, method_names)
    method_names.all? do |method_name|
      object.respond_to?(method_name)
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
journaled-2.0.3 app/models/journaled/writer.rb
journaled-2.0.2 app/models/journaled/writer.rb
journaled-2.0.1 app/models/journaled/writer.rb
journaled-2.0.0 app/models/journaled/writer.rb
journaled-2.0.0.rc1 app/models/journaled/writer.rb
journaled-2.0.0.alpha1 app/models/journaled/writer.rb
journaled-1.0.1 app/models/journaled/writer.rb
journaled-1.0.0 app/models/journaled/writer.rb