Class: Acta::Event

Inherits:
Model
  • Object
show all
Defined in:
lib/acta/event.rb

Constant Summary collapse

ENVELOPE_KEYS =
%i[ uuid occurred_at recorded_at actor ].freeze

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Model

attribute, from_acta_hash, #to_acta_hash

Constructor Details

#initialize(**attrs) ⇒ Event

Returns a new instance of Event.

Raises:



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/acta/event.rb', line 12

def initialize(**attrs)
  envelope = attrs.slice(*ENVELOPE_KEYS)
  payload = attrs.except(*ENVELOPE_KEYS)

  @uuid = envelope[:uuid] || SecureRandom.uuid
  @occurred_at = envelope[:occurred_at] || Time.current
  @recorded_at = envelope[:recorded_at]
  @actor = envelope.key?(:actor) ? envelope[:actor] : Acta::Current.actor

  super(**payload)

  raise InvalidEvent, self unless valid?
end

Class Attribute Details

.stream_key_attributeObject (readonly)

Returns the value of attribute stream_key_attribute.



52
53
54
# File 'lib/acta/event.rb', line 52

def stream_key_attribute
  @stream_key_attribute
end

.stream_typeObject (readonly)

Returns the value of attribute stream_type.



52
53
54
# File 'lib/acta/event.rb', line 52

def stream_type
  @stream_type
end

Instance Attribute Details

#actorObject

Returns the value of attribute actor.



8
9
10
# File 'lib/acta/event.rb', line 8

def actor
  @actor
end

#occurred_atObject

Returns the value of attribute occurred_at.



8
9
10
# File 'lib/acta/event.rb', line 8

def occurred_at
  @occurred_at
end

#recorded_atObject

Returns the value of attribute recorded_at.



8
9
10
# File 'lib/acta/event.rb', line 8

def recorded_at
  @recorded_at
end

#uuidObject

Returns the value of attribute uuid.



8
9
10
# File 'lib/acta/event.rb', line 8

def uuid
  @uuid
end

Class Method Details

.event_typeObject



38
39
40
# File 'lib/acta/event.rb', line 38

def self.event_type
  name
end

.event_versionObject



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

def self.event_version
  1
end

.from_acta_record(envelope:, payload:) ⇒ Object

Reconstructs an event from a stored record. Routes payload values through their type’s ‘deserialize` (so encrypted attributes decrypt back to plaintext) before construction.



69
70
71
72
73
74
75
76
77
78
# File 'lib/acta/event.rb', line 69

def self.from_acta_record(envelope:, payload:)
  types = attribute_types
  decoded = (payload || {}).each_with_object({}) do |(k, v), acc|
    key = k.to_s
    next unless types.key?(key)

    acc[key.to_sym] = types[key].deserialize(v)
  end
  new(**envelope, **decoded)
end

.stream(type, key:) ⇒ Object



46
47
48
49
# File 'lib/acta/event.rb', line 46

def self.stream(type, key:)
  @stream_type = type.to_s
  @stream_key_attribute = key
end

Instance Method Details

#event_typeObject



26
27
28
# File 'lib/acta/event.rb', line 26

def event_type
  self.class.event_type
end

#event_versionObject



30
31
32
# File 'lib/acta/event.rb', line 30

def event_version
  self.class.event_version
end

#payload_hashObject



34
35
36
# File 'lib/acta/event.rb', line 34

def payload_hash
  to_acta_hash
end

#stream_keyObject



59
60
61
62
63
64
# File 'lib/acta/event.rb', line 59

def stream_key
  attribute = self.class.stream_key_attribute
  return nil if attribute.nil?

  public_send(attribute)
end

#stream_typeObject



55
56
57
# File 'lib/acta/event.rb', line 55

def stream_type
  self.class.stream_type
end