Class: EventSourcery::Event

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/event_sourcery/event.rb

Overview

Represents an Event

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id: nil, uuid: SecureRandom.uuid, aggregate_id: nil, type: nil, body: nil, version: nil, created_at: nil, correlation_id: nil, causation_id: nil) ⇒ Event

Returns a new instance of Event.

Parameters:

  • id (Integer) (defaults to: nil)

    Optional. Unique identifier at the persistent layer. By default this will be set by the underlying persistence layer when persisting the event.

  • uuid (String) (defaults to: SecureRandom.uuid)

    UUID as a string. Optional. Unique identifier for this event. A random UUID will be generated by default.

  • aggregate_id (String) (defaults to: nil)

    UUID as a string. Aggregate instance UUID to which this event belongs to.

  • type (Class) (defaults to: nil)

    Optional. Event type. type will be used by default.

  • version (String) (defaults to: nil)

    Optional. Event's aggregate version. Used by some event stores to guard against concurrency errors.

  • created_at (Time) (defaults to: nil)

    Optional. Created at timestamp (in UTC) for the event.

  • correlation_id (String) (defaults to: nil)

    Optional. UUID attached to the event that allows reference to a particular transaction or event chain. This value is often supplied as part of a command issued by clients.

  • causation_id (String) (defaults to: nil)

    Optional. UUID of the event that caused this event.


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/event_sourcery/event.rb', line 53

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 && aggregate_id.to_str
  @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

Instance Attribute Details

#aggregate_idString (readonly)

Returns aggregate instance UUID to which this event belongs to.

Returns:

  • (String)

    aggregate instance UUID to which this event belongs to.


23
24
25
# File 'lib/event_sourcery/event.rb', line 23

def aggregate_id
  @aggregate_id
end

#bodyHash (readonly)

Returns Content of the event body.

Returns:

  • (Hash)

    Content of the event body.


29
30
31
# File 'lib/event_sourcery/event.rb', line 29

def body
  @body
end

#causation_idString (readonly)

Returns UUID of the event that caused this event.

Returns:

  • (String)

    UUID of the event that caused this event.


41
42
43
# File 'lib/event_sourcery/event.rb', line 41

def causation_id
  @causation_id
end

#correlation_idString (readonly)

Returns UUID attached to the event that allows reference to a particular transaction or event chain. This value is often supplied as part of a command issued by clients.

Returns:

  • (String)

    UUID attached to the event that allows reference to a particular transaction or event chain. This value is often supplied as part of a command issued by clients.


38
39
40
# File 'lib/event_sourcery/event.rb', line 38

def correlation_id
  @correlation_id
end

#created_atTime (readonly)

Returns Created at timestamp (in UTC) for the event.

Returns:

  • (Time)

    Created at timestamp (in UTC) for the event.


35
36
37
# File 'lib/event_sourcery/event.rb', line 35

def created_at
  @created_at
end

#idInteger (readonly)

Returns unique identifier at the persistent layer.

Returns:

  • (Integer)

    unique identifier at the persistent layer


17
18
19
# File 'lib/event_sourcery/event.rb', line 17

def id
  @id
end

#typeObject (readonly)

Returns event type.

Returns:

  • event type


26
27
28
# File 'lib/event_sourcery/event.rb', line 26

def type
  @type
end

#uuidString (readonly)

Returns unique identifier (UUID) for this event.

Returns:

  • (String)

    unique identifier (UUID) for this event.


20
21
22
# File 'lib/event_sourcery/event.rb', line 20

def uuid
  @uuid
end

#versionString (readonly)

Returns event version. Used by some event stores to guard against concurrency errors.

Returns:

  • (String)

    event version. Used by some event stores to guard against concurrency errors.


32
33
34
# File 'lib/event_sourcery/event.rb', line 32

def version
  @version
end

Class Method Details

.typeObject

Event type

Will return `nil` if called on an instance of EventSourcery::Event.


9
10
11
12
13
# File 'lib/event_sourcery/event.rb', line 9

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

Instance Method Details

#<=>(other) ⇒ Object


86
87
88
# File 'lib/event_sourcery/event.rb', line 86

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

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)

82
83
84
# File 'lib/event_sourcery/event.rb', line 82

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

#hashObject


78
79
80
# File 'lib/event_sourcery/event.rb', line 78

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

#persisted?Boolean

Is this event persisted?

Returns:

  • (Boolean)

74
75
76
# File 'lib/event_sourcery/event.rb', line 74

def persisted?
  !id.nil?
end

#to_hObject

returns a hash of the event attributes

Returns:

  • Hash


114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/event_sourcery/event.rb', line 114

def to_h
  {
    id:             id,
    uuid:           uuid,
    aggregate_id:   aggregate_id,
    type:           type,
    body:           body,
    version:        version,
    created_at:     created_at,
    correlation_id: correlation_id,
    causation_id:   causation_id,
  }
end

#with(**attributes) ⇒ Object

create a new event identical to the old event except for the provided changes

Examples:

old_event = EventSourcery::Event.new(type: "item_added", causation_id: nil)
new_event = old_event.with(causation_id: "05781bd6-796a-4a58-8573-b109f683fd99")

new_event.type # => "item_added"
new_event.causation_id # => "05781bd6-796a-4a58-8573-b109f683fd99"

old_event.type # => "item_added"
old_event.causation_id # => nil

# Of course, with can accept any number of event attributes:

 new_event = old_event.with(id: 42, version: 77, body: { 'attr' => 'value' })

Parameters:

  • attributes (Hash)

Returns:

  • Event


107
108
109
# File 'lib/event_sourcery/event.rb', line 107

def with(**attributes)
  self.class.new(**to_h.merge!(attributes))
end