Class: Funes::Event
- Inherits:
-
Object
- Object
- Funes::Event
- Includes:
- ActiveModel::Attributes, ActiveModel::Model
- 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 two 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.
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:
Instance Attribute Summary collapse
-
#adjacent_state_errors ⇒ ActiveModel::Errors
Validation errors from consistency projections.
-
#event_errors ⇒ ActiveModel::Errors?
The event's own validation errors (internal use).
Instance Method Summary collapse
-
#errors ⇒ ActiveModel::Errors
Get all validation errors (both event and state errors merged).
-
#inspect ⇒ String
Custom string representation of the event.
-
#own_errors ⇒ ActiveModel::Errors
Get the event's own validation errors (excluding state errors).
-
#state_errors ⇒ ActiveModel::Errors
Get validation errors from consistency projections.
-
#valid? ⇒ Boolean
Check if the event is valid.
Instance Attribute Details
#adjacent_state_errors ⇒ ActiveModel::Errors
Returns Validation errors from consistency projections.
49 50 51 |
# File 'app/models/funes/event.rb', line 49 def adjacent_state_errors @adjacent_state_errors end |
#event_errors ⇒ ActiveModel::Errors?
Returns The event's own validation errors (internal use).
53 54 55 |
# File 'app/models/funes/event.rb', line 53 def event_errors @event_errors end |
Instance Method Details
#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.
126 127 128 129 130 131 132 133 134 135 |
# File 'app/models/funes/event.rb', line 126 def errors return super if @event_errors.nil? tmp_errors = ActiveModel::Errors.new(nil) tmp_errors.merge!(event_errors) adjacent_state_errors.each do |error| tmp_errors.add(:base, "#{I18n.t("funes.events.led_to_invalid_state_prefix")}: #{error.}") end tmp_errors end |
#inspect ⇒ String
Custom string representation of the event.
73 74 75 |
# File 'app/models/funes/event.rb', line 73 def inspect "<#{self.class.name}: #{attributes}>" end |
#own_errors ⇒ ActiveModel::Errors
Get the event's own validation errors (excluding state errors).
112 113 114 |
# File 'app/models/funes/event.rb', line 112 def own_errors event_errors || errors 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.
101 102 103 |
# File 'app/models/funes/event.rb', line 101 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).
87 88 89 |
# File 'app/models/funes/event.rb', line 87 def valid? super && (adjacent_state_errors.nil? || adjacent_state_errors.empty?) end |