Class: ActiveEventStore::DomainEvent

Inherits:
RubyEventStore::Mappers::Transformation::DomainEvent
  • Object
show all
Defined in:
lib/active_event_store/domain_event.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mapping:) ⇒ DomainEvent

Returns a new instance of DomainEvent.



6
7
8
# File 'lib/active_event_store/domain_event.rb', line 6

def initialize(mapping:)
  @mapping = mapping
end

Instance Attribute Details

#mappingObject (readonly)

Returns the value of attribute mapping.



5
6
7
# File 'lib/active_event_store/domain_event.rb', line 5

def mapping
  @mapping
end

Instance Method Details

#dump(event) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/active_event_store/domain_event.rb', line 10

def dump(event)
  # lazily add type to mapping
  # NOTE: use class name instead of a class to handle code reload
  # in development (to avoid accessing orphaned classes)
  mapping.register(event.event_type, event.class.name) unless mapping.exist?(event.event_type)

   = event..dup.to_h
  timestamp = [:timestamp]
  valid_at = [:valid_at]
  RubyEventStore::Record.new(
    event_id: event.event_id,
    metadata: ,
    data: event.data,
    event_type: event.event_type,
    timestamp: timestamp,
    valid_at: valid_at
  )
end

#load(record) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/active_event_store/domain_event.rb', line 29

def load(record)
  event_class = mapping.fetch(record.event_type) {
    raise "Don't know how to deserialize event: \"#{record.event_type}\". " \
          "Add explicit mapping: ActiveEventStore.mapping.register \"#{record.event_type}\", \"<Class Name>\""
  }

  Object
    .const_get(event_class)
    .new(
      **record.data.symbolize_keys,
      event_id: record.event_id,
      metadata: record..merge(timestamp: record.timestamp, valid_at: record.valid_at)
    )
end