Class: FourthDimensional::Event
- Inherits:
-
Object
- Object
- FourthDimensional::Event
- Defined in:
- lib/fourth_dimensional/event.rb
Overview
FourthDimensional::Event
Events act as a log primarily focused around an aggregate. When persisted it will use the underscored version of the class name as the event_type
.
module Posts
PostAdded = Class.new(FourthDimensional::Event)
end
It is recommended practice to add delegating methods to the attributes within data
. These aliases will make it easier to see what’s being stored from the codebase.
module Posts
class PostAdded < FourthDimensional::Event
def title
data.fetch('title')
end
def body
data.fetch('body')
end
end
end
Instance Attribute Summary collapse
-
#aggregate_id ⇒ Object
readonly
Returns the value of attribute aggregate_id.
-
#causation_id ⇒ Object
readonly
surrounding event associations.
-
#correlation_id ⇒ Object
readonly
surrounding event associations.
-
#created_at ⇒ Object
readonly
persisted event attributes.
-
#data ⇒ Object
readonly
hash of data with stringified keys.
-
#id ⇒ Object
readonly
persisted event attributes.
-
#metadata ⇒ Object
readonly
hash of data with stringified keys.
-
#updated_at ⇒ Object
readonly
persisted event attributes.
-
#version ⇒ Object
readonly
persisted event attributes.
Instance Method Summary collapse
-
#initialize(aggregate_id:, id: nil, version: nil, correlation_id: nil, causation_id: nil, created_at: nil, updated_at: nil, data: nil, metadata: nil) ⇒ Event
constructor
Initializes an event with the required
aggregate_id
and optionaldata
andmetadata
. -
#type ⇒ Object
Underscored event type.
Constructor Details
#initialize(aggregate_id:, id: nil, version: nil, correlation_id: nil, causation_id: nil, created_at: nil, updated_at: nil, data: nil, metadata: nil) ⇒ Event
Initializes an event with the required aggregate_id
and optional data
and metadata
.
event = MyEvent.new(aggregate_id: '1-2-3')
event.aggregate_id # => '1-2-3'
event.data # => {}
event. # => {}
data
and metadata
should be hashes and the keys will transformed into strings to accommodate deserializing the values from json.
event = MyEvent.new(aggregate_id: '1-2-3',
version: 1,
data: { one: 1 },
metadata: { two: 2 })
event.version # => 1
event.data # => { 'one' => 1 }
event. # => { 'two' => 2 }
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/fourth_dimensional/event.rb', line 59 def initialize(aggregate_id:, id: nil, version: nil, correlation_id: nil, causation_id: nil, created_at: nil, updated_at: nil, data: nil, metadata: nil) @aggregate_id = aggregate_id @id = id @version = version @correlation_id = correlation_id @causation_id = causation_id @created_at = created_at @updated_at = updated_at @data = (data || {}).transform_keys(&:to_s) @metadata = ( || {}).transform_keys(&:to_s) end |
Instance Attribute Details
#aggregate_id ⇒ Object (readonly)
Returns the value of attribute aggregate_id.
30 31 32 |
# File 'lib/fourth_dimensional/event.rb', line 30 def aggregate_id @aggregate_id end |
#causation_id ⇒ Object (readonly)
surrounding event associations
36 37 38 |
# File 'lib/fourth_dimensional/event.rb', line 36 def causation_id @causation_id end |
#correlation_id ⇒ Object (readonly)
surrounding event associations
36 37 38 |
# File 'lib/fourth_dimensional/event.rb', line 36 def correlation_id @correlation_id end |
#created_at ⇒ Object (readonly)
persisted event attributes
39 40 41 |
# File 'lib/fourth_dimensional/event.rb', line 39 def created_at @created_at end |
#data ⇒ Object (readonly)
hash of data with stringified keys
33 34 35 |
# File 'lib/fourth_dimensional/event.rb', line 33 def data @data end |
#id ⇒ Object (readonly)
persisted event attributes
39 40 41 |
# File 'lib/fourth_dimensional/event.rb', line 39 def id @id end |
#metadata ⇒ Object (readonly)
hash of data with stringified keys
33 34 35 |
# File 'lib/fourth_dimensional/event.rb', line 33 def @metadata end |
#updated_at ⇒ Object (readonly)
persisted event attributes
39 40 41 |
# File 'lib/fourth_dimensional/event.rb', line 39 def updated_at @updated_at end |
#version ⇒ Object (readonly)
persisted event attributes
39 40 41 |
# File 'lib/fourth_dimensional/event.rb', line 39 def version @version end |
Instance Method Details
#type ⇒ Object
Underscored event type
Products::Events::Created.new.type # => "products/events/created"
82 83 84 |
# File 'lib/fourth_dimensional/event.rb', line 82 def type self.class.name.underscore end |