Sha256: 8206f651cf14b4a4151f42b46a3c53783b46d85af598c668e73fc8f90a5f0000

Contents?: true

Size: 1.22 KB

Versions: 1

Compression:

Stored size: 1.22 KB

Contents

module EventSourcery
  class Event
    include Comparable

    def self.type
      unless self == Event
        EventSourcery.config.event_type_serializer.serialize(self)
      end
    end

    attr_reader :id, :uuid, :aggregate_id, :type, :body, :version, :created_at, :correlation_id, :causation_id

    def initialize(id: nil,
                   uuid: SecureRandom.uuid,
                   aggregate_id: nil,
                   type: nil,
                   body: nil,
                   version: nil,
                   created_at: nil,
                   correlation_id: nil,
                   causation_id: nil)
      @id = id
      @uuid = uuid && uuid.downcase
      @aggregate_id = aggregate_id
      @type = self.class.type || type.to_s
      @body = body ? EventSourcery::EventBodySerializer.serialize(body) : {}
      @version = version ? Integer(version) : nil
      @created_at = created_at
      @correlation_id = correlation_id
      @causation_id = causation_id
    end

    def persisted?
      !id.nil?
    end

    def hash
      [self.class, uuid].hash
    end

    def eql?(other)
      instance_of?(other.class) && uuid.eql?(other.uuid)
    end

    def <=>(other)
      id <=> other.id if other.is_a? Event
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
event_sourcery-0.13.0 lib/event_sourcery/event.rb