Sha256: 06f8624b6a5642345b337328d045bae7f2e1024d7c9b5a3c5d646dc59e681fa9

Contents?: true

Size: 1.21 KB

Versions: 6

Compression:

Stored size: 1.21 KB

Contents

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_app_name
    Journaled.default_app_name
  end

  private

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

    def journaled_attributes
      @journaled_attributes ||= []
    end

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

  included do
    cattr_accessor(:journaled_enqueue_opts, instance_writer: false) { {} }

    journal_attributes :id, :event_type, :created_at
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
journaled-3.1.0 app/models/journaled/event.rb
journaled-3.0.0 app/models/journaled/event.rb
journaled-2.5.0 app/models/journaled/event.rb
journaled-2.4.0 app/models/journaled/event.rb
journaled-2.3.1 app/models/journaled/event.rb
journaled-2.3.0 app/models/journaled/event.rb