Class: Acta::Event
Constant Summary collapse
- ENVELOPE_KEYS =
%i[ uuid occurred_at recorded_at actor ].freeze
Class Attribute Summary collapse
-
.stream_key_attribute ⇒ Object
readonly
Returns the value of attribute stream_key_attribute.
-
.stream_type ⇒ Object
readonly
Returns the value of attribute stream_type.
Instance Attribute Summary collapse
-
#actor ⇒ Object
Returns the value of attribute actor.
-
#occurred_at ⇒ Object
Returns the value of attribute occurred_at.
-
#recorded_at ⇒ Object
Returns the value of attribute recorded_at.
-
#uuid ⇒ Object
Returns the value of attribute uuid.
Class Method Summary collapse
- .event_type ⇒ Object
- .event_version ⇒ Object
-
.from_acta_record(envelope:, payload:) ⇒ Object
Reconstructs an event from a stored record.
- .stream(type, key:) ⇒ Object
Instance Method Summary collapse
- #event_type ⇒ Object
- #event_version ⇒ Object
-
#initialize(**attrs) ⇒ Event
constructor
A new instance of Event.
- #payload_hash ⇒ Object
- #stream_key ⇒ Object
- #stream_type ⇒ Object
Methods inherited from Model
attribute, from_acta_hash, #to_acta_hash
Constructor Details
#initialize(**attrs) ⇒ Event
Returns a new instance of Event.
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_attribute ⇒ Object (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_type ⇒ Object (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
#actor ⇒ Object
Returns the value of attribute actor.
8 9 10 |
# File 'lib/acta/event.rb', line 8 def actor @actor end |
#occurred_at ⇒ Object
Returns the value of attribute occurred_at.
8 9 10 |
# File 'lib/acta/event.rb', line 8 def occurred_at @occurred_at end |
#recorded_at ⇒ Object
Returns the value of attribute recorded_at.
8 9 10 |
# File 'lib/acta/event.rb', line 8 def recorded_at @recorded_at end |
#uuid ⇒ Object
Returns the value of attribute uuid.
8 9 10 |
# File 'lib/acta/event.rb', line 8 def uuid @uuid end |
Class Method Details
.event_type ⇒ Object
38 39 40 |
# File 'lib/acta/event.rb', line 38 def self.event_type name end |
.event_version ⇒ Object
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_type ⇒ Object
26 27 28 |
# File 'lib/acta/event.rb', line 26 def event_type self.class.event_type end |
#event_version ⇒ Object
30 31 32 |
# File 'lib/acta/event.rb', line 30 def event_version self.class.event_version end |
#payload_hash ⇒ Object
34 35 36 |
# File 'lib/acta/event.rb', line 34 def payload_hash to_acta_hash end |
#stream_key ⇒ Object
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_type ⇒ Object
55 56 57 |
# File 'lib/acta/event.rb', line 55 def stream_type self.class.stream_type end |