Class: EventSourcery::Event
- Inherits:
-
Object
- Object
- EventSourcery::Event
- Includes:
- Comparable
- Defined in:
- lib/event_sourcery/event.rb
Overview
Represents an Event
Instance Attribute Summary collapse
-
#aggregate_id ⇒ String
readonly
Aggregate instance UUID to which this event belongs to.
-
#body ⇒ Hash
readonly
Content of the event body.
-
#causation_id ⇒ String
readonly
UUID of the event that caused this event.
-
#correlation_id ⇒ String
readonly
UUID attached to the event that allows reference to a particular transaction or event chain.
-
#created_at ⇒ Time
readonly
Created at timestamp (in UTC) for the event.
-
#id ⇒ Integer
readonly
Unique identifier at the persistent layer.
-
#type ⇒ Object
readonly
Event type.
-
#uuid ⇒ String
readonly
Unique identifier (UUID) for this event.
-
#version ⇒ String
readonly
Event version.
Class Method Summary collapse
-
.type ⇒ Object
Event type.
-
.upcast(event) ⇒ Object
Use this method to add “upcasting” to your events.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #eql?(other) ⇒ Boolean
- #hash ⇒ Object
-
#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
constructor
A new instance of Event.
-
#to_h ⇒ Object
returns a hash of the event attributes.
-
#with(event_class: self.class, **attributes) ⇒ Object
create a new event identical to the old event except for the provided changes.
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.
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_id ⇒ String (readonly)
Returns 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 |
#body ⇒ Hash (readonly)
Returns Content of the event body.
54 55 56 |
# File 'lib/event_sourcery/event.rb', line 54 def body @body end |
#causation_id ⇒ String (readonly)
Returns 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_id ⇒ String (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.
63 64 65 |
# File 'lib/event_sourcery/event.rb', line 63 def correlation_id @correlation_id end |
#created_at ⇒ Time (readonly)
Returns 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 |
#id ⇒ Integer (readonly)
Returns unique identifier at the persistent layer.
42 43 44 |
# File 'lib/event_sourcery/event.rb', line 42 def id @id end |
#type ⇒ Object (readonly)
Returns event type.
51 52 53 |
# File 'lib/event_sourcery/event.rb', line 51 def type @type end |
#uuid ⇒ String (readonly)
Returns unique identifier (UUID) for this event.
45 46 47 |
# File 'lib/event_sourcery/event.rb', line 45 def uuid @uuid end |
#version ⇒ String (readonly)
Returns 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
.type ⇒ Object
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.
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
102 103 104 |
# File 'lib/event_sourcery/event.rb', line 102 def eql?(other) instance_of?(other.class) && uuid.eql?(other.uuid) end |
#hash ⇒ Object
98 99 100 |
# File 'lib/event_sourcery/event.rb', line 98 def hash [self.class, uuid].hash end |
#to_h ⇒ Object
returns a hash of the event attributes
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
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 |