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.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/event_sourcery/event.rb', line 78

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.



48
49
50
# File 'lib/event_sourcery/event.rb', line 48

def aggregate_id
  @aggregate_id
end

#bodyHash (readonly)

Returns Content of the event body.

Returns:

  • (Hash)

    Content of the event body.



54
55
56
# File 'lib/event_sourcery/event.rb', line 54

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.



66
67
68
# File 'lib/event_sourcery/event.rb', line 66

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.



63
64
65
# File 'lib/event_sourcery/event.rb', line 63

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.



60
61
62
# File 'lib/event_sourcery/event.rb', line 60

def created_at
  @created_at
end

#idInteger (readonly)

Returns unique identifier at the persistent layer.

Returns:

  • (Integer)

    unique identifier at the persistent layer



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

def id
  @id
end

#typeObject (readonly)

Returns event type.

Returns:

  • event type



51
52
53
# File 'lib/event_sourcery/event.rb', line 51

def type
  @type
end

#uuidString (readonly)

Returns unique identifier (UUID) for this event.

Returns:

  • (String)

    unique identifier (UUID) for this event.



45
46
47
# File 'lib/event_sourcery/event.rb', line 45

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.



57
58
59
# File 'lib/event_sourcery/event.rb', line 57

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

.upcast(event) ⇒ Object

Use this method to add “upcasting” to your events.

To upcast your events override the ‘upcast` class method on your event. The `upcast` method will be passed the event allowing you to modify it before it is passed to your event processors.

A good place to start is using the ‘Event#with` method that allows you to easily change attributes.

Examples:

Foo = Class.new(EventSourcery::Event) do
  def self.upcast(event)
    body = event.body

    body['bar'] ||= 'baz'

    event.with(body: body)
  end
end

Parameters:

  • Event

Returns:

  • Event



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

def self.upcast(event)
  event
end

Instance Method Details

#<=>(other) ⇒ Object



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

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

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/event_sourcery/event.rb', line 102

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

#hashObject



98
99
100
# File 'lib/event_sourcery/event.rb', line 98

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

#to_hObject

returns a hash of the event attributes

Returns:

  • Hash



144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/event_sourcery/event.rb', line 144

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(event_class: self.class, **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' })

# When using typed events you can also override the event class:

new_event = old_event.with(event_class: ItemRemoved)
new_event.type # => "item_removed"
new_event.class # => ItemRemoved

Parameters:

  • attributes (Hash)

Returns:

  • Event



133
134
135
136
137
138
139
# File 'lib/event_sourcery/event.rb', line 133

def with(event_class: self.class, **attributes)
  if self.class != Event && !attributes[:type].nil? && attributes[:type] != type
    raise Error, 'When using typed events change the type by changing the event class.'
  end

  event_class.new(**to_h.merge!(attributes))
end