Sha256: 1755e33845475acf912894feb53ce63a8d79f65ac9a1226f2d1277e37d8bce65

Contents?: true

Size: 1.5 KB

Versions: 2

Compression:

Stored size: 1.5 KB

Contents

# frozen_string_literal: true

module Journaled::Event
  extend ActiveSupport::Concern

  def journal!
    Journaled::Writer.new(journaled_event: self).journal!
  end

  # Base attributes

  def id
    @id ||= SecureRandom.uuid
  end

  def event_type
    @event_type ||= self.class.event_type
  end

  def created_at
    @created_at ||= Time.zone.now
  end

  # Event metadata and configuration (not serialized)

  def journaled_schema_name
    self.class.to_s.underscore
  end

  def journaled_attributes
    self.class.public_send(:journaled_attributes).each_with_object({}) do |attribute, memo|
      memo[attribute] = public_send(attribute)
    end
  end

  def journaled_partition_key
    event_type
  end

  def journaled_stream_name
    Journaled.default_stream_name
  end

  def tagged?
    false
  end

  private

  class_methods do
    def journal_attributes(*args, enqueue_with: {}, tagged: false)
      journaled_attributes.concat(args)
      journaled_enqueue_opts.merge!(enqueue_with)

      include Tagged if tagged
    end

    def journaled_attributes
      @journaled_attributes ||= []
    end

    def event_type
      name.underscore.parameterize(separator: '_')
    end
  end

  included do
    class_attribute :journaled_enqueue_opts, default: {}

    journal_attributes :id, :event_type, :created_at
  end

  module Tagged
    extend ActiveSupport::Concern

    included do
      journaled_attributes << :tags
    end

    def tags
      Journaled::Current.tags
    end

    def tagged?
      true
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
journaled-6.0.0 app/models/journaled/event.rb
journaled-5.3.2 app/models/journaled/event.rb