Class: FourthDimensional::Event

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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_idObject (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_idObject (readonly)

surrounding event associations



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

def causation_id
  @causation_id
end

#correlation_idObject (readonly)

surrounding event associations



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

def correlation_id
  @correlation_id
end

#created_atObject (readonly)

persisted event attributes



39
40
41
# File 'lib/fourth_dimensional/event.rb', line 39

def created_at
  @created_at
end

#dataObject (readonly)

hash of data with stringified keys



33
34
35
# File 'lib/fourth_dimensional/event.rb', line 33

def data
  @data
end

#idObject (readonly)

persisted event attributes



39
40
41
# File 'lib/fourth_dimensional/event.rb', line 39

def id
  @id
end

#metadataObject (readonly)

hash of data with stringified keys



33
34
35
# File 'lib/fourth_dimensional/event.rb', line 33

def 
  @metadata
end

#updated_atObject (readonly)

persisted event attributes



39
40
41
# File 'lib/fourth_dimensional/event.rb', line 39

def updated_at
  @updated_at
end

#versionObject (readonly)

persisted event attributes



39
40
41
# File 'lib/fourth_dimensional/event.rb', line 39

def version
  @version
end

Instance Method Details

#typeObject

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