Class: Funes::Event
- Inherits:
-
Object
- Object
- Funes::Event
- Includes:
- ActiveModel::Attributes, ActiveModel::Model, Associations, Inspection
- Defined in:
- app/models/funes/event.rb
Overview
Base class for all events in the Funes event sourcing framework.
Events are immutable facts that represent something that happened in the system. They use ActiveModel for attributes and validations, making them familiar to Rails developers.
Event Validation
Events support three types of validation:
- Own validation: Standard ActiveModel validations defined on the event class itself.
- Adjacent state validation: Validation errors from consistency projections that check if the event would lead to an invalid state.
- Interpretation errors: Errors added via
event.errors.add(...)inside interpretation blocks. When used in a consistency projection, these are automatically transferred tointerpretation_errorsand cause the event to be rejected.
The valid? method returns true only if both validations pass. The errors method
merges both types of errors for display.
Defining Events
Events inherit from Funes::Event and define attributes using ActiveModel::Attributes:
Associating Events With Other Models
Use refers_to to relate an event to another model. Only the referenced record's id is stored
in the event payload; the record itself is loaded lazily and is readable at interpretation time:
Instance Attribute Summary collapse
-
#_adjacent_state_errors ⇒ Object
Returns the value of attribute _adjacent_state_errors.
-
#_event_entry ⇒ Funes::EventEntry?
The persisted EventEntry record (internal use).
-
#_interpretation_errors ⇒ Object
Returns the value of attribute _interpretation_errors.
-
#adjacent_state_errors ⇒ ActiveModel::Errors
Validation errors from consistency projections.
-
#interpretation_errors ⇒ ActiveModel::Errors
Explicit rejection errors from consistency projection interpretation blocks.
Instance Method Summary collapse
-
#created_at ⇒ Time?
Returns the timestamp when the event was persisted.
-
#errors ⇒ ActiveModel::Errors
Get all validation errors (both event and state errors merged).
-
#invalid? ⇒ Boolean
Check if the event is invalid.
-
#occurred_at ⇒ Time?
Returns the timestamp when the event actually occurred.
-
#own_errors ⇒ ActiveModel::Errors
Get the event's own validation errors (excluding state errors).
-
#persisted? ⇒ Boolean
Check if the event has been persisted to the database.
-
#state_errors ⇒ ActiveModel::Errors
Get validation errors from consistency projections.
-
#valid? ⇒ Boolean
Check if the event is valid.
-
#version ⇒ Integer?
Returns the version number of the event within its stream.
Methods included from Inspection
#attribute_for_inspect, #full_inspect, #inspect, #pretty_print
Instance Attribute Details
#_adjacent_state_errors ⇒ Object
Returns the value of attribute _adjacent_state_errors.
72 73 74 |
# File 'app/models/funes/event.rb', line 72 def _adjacent_state_errors @_adjacent_state_errors end |
#_event_entry ⇒ Funes::EventEntry?
Returns The persisted EventEntry record (internal use).
80 81 82 |
# File 'app/models/funes/event.rb', line 80 def _event_entry @_event_entry end |
#_interpretation_errors ⇒ Object
Returns the value of attribute _interpretation_errors.
76 77 78 |
# File 'app/models/funes/event.rb', line 76 def _interpretation_errors @_interpretation_errors end |
#adjacent_state_errors ⇒ ActiveModel::Errors
Returns Validation errors from consistency projections.
72 |
# File 'app/models/funes/event.rb', line 72 attr_accessor :_adjacent_state_errors |
#interpretation_errors ⇒ ActiveModel::Errors
Returns Explicit rejection errors from consistency projection interpretation blocks.
76 |
# File 'app/models/funes/event.rb', line 76 attr_accessor :_interpretation_errors |
Instance Method Details
#created_at ⇒ Time?
Returns the timestamp when the event was persisted.
128 |
# File 'app/models/funes/event.rb', line 128 def created_at = _event_entry&.created_at |
#errors ⇒ ActiveModel::Errors
Get all validation errors (both event and state errors merged).
This method merges the event's own validation errors with any errors from consistency projections, prefixing state errors with a localized message.
219 220 221 222 223 224 225 226 227 228 |
# File 'app/models/funes/event.rb', line 219 def errors return super unless !_adjacent_state_errors.empty? || !_interpretation_errors.empty? tmp_errors = ActiveModel::Errors.new(self) tmp_errors.merge!(super) merge_errors_into(tmp_errors, _adjacent_state_errors, state_errors: true) merge_errors_into(tmp_errors, _interpretation_errors) tmp_errors end |
#invalid? ⇒ Boolean
Check if the event is invalid.
An event is invalid if any of its own validations fail, it leads to an invalid state (adjacent_state_errors), or it has been explicitly rejected via interpretation_errors.
178 |
# File 'app/models/funes/event.rb', line 178 def invalid? = !valid? |
#occurred_at ⇒ Time?
Returns the timestamp when the event actually occurred.
When an event is recorded retroactively (with an explicit at: on append),
this returns the actual time of the event. Otherwise, it equals created_at.
140 |
# File 'app/models/funes/event.rb', line 140 def occurred_at = _event_entry&.occurred_at |
#own_errors ⇒ ActiveModel::Errors
Get the event's own validation errors (excluding state errors).
201 202 203 204 205 206 207 |
# File 'app/models/funes/event.rb', line 201 def own_errors tmp_errors = ActiveModel::Errors.new(self) tmp_errors.merge!(base_errors) merge_errors_into(tmp_errors, _interpretation_errors) tmp_errors end |
#persisted? ⇒ Boolean
Check if the event has been persisted to the database.
An event is considered persisted if it was either saved via EventStream#append or
reconstructed from an Funes::EventEntry via to_klass_instance.
114 115 116 |
# File 'app/models/funes/event.rb', line 114 def persisted? _event_entry.present? end |
#state_errors ⇒ ActiveModel::Errors
Get validation errors from consistency projections.
These are errors that indicate the event would lead to an invalid state, even if the event itself is valid.
190 191 192 |
# File 'app/models/funes/event.rb', line 190 def state_errors _adjacent_state_errors end |
#valid? ⇒ Boolean
Check if the event is valid.
An event is valid only if both its own validations pass AND it doesn't lead to an invalid state (no adjacent_state_errors from consistency projections).
164 165 166 |
# File 'app/models/funes/event.rb', line 164 def valid? super && _adjacent_state_errors.empty? && _interpretation_errors.empty? end |
#version ⇒ Integer?
Returns the version number of the event within its stream.
Each event in a stream gets an incrementing version number used for optimistic concurrency control. The version is assigned when the event is persisted.
152 |
# File 'app/models/funes/event.rb', line 152 def version = _event_entry&.version |