class RubyEventStore::Event
Data structure representing an event
Attributes
Public Class Methods
Source
# File lib/ruby_event_store/event.rb, line 16 def initialize(event_id: SecureRandom.uuid, metadata: nil, data: {}) @event_id = event_id.to_s @metadata = Metadata.new(metadata.to_h) @data = data end
Instantiates a new event
@param event_id
[String] event id @param data [Hash] event data which belong to your application domain @param metadata [Hash] event metadata which are technical and not
part of your domain such as remote_ip, request_id, correlation_id, causation_id etc.
@return [Event]
Public Instance Methods
Source
# File lib/ruby_event_store/event.rb, line 60 def ==(other_event) other_event.instance_of?(self.class) && other_event.event_type.eql?(event_type) && other_event.event_id.eql?(event_id) && other_event.data.eql?(data) end
Two events are equal if:
-
they are of the same class
-
have identical event type
-
have identical event id
-
have identical data (verified with eql? method)
@param other_event [Event, Object] object to compare
Event
equality ignores metadata! @return [TrueClass, FalseClass]
Source
# File lib/ruby_event_store/event.rb, line 101 def causation_id metadata[:causation_id] end
Reads causation_id
from metadata. {railseventstore.org/docs/correlation_causation/ Find out more}
@return [String, nil]
Source
# File lib/ruby_event_store/event.rb, line 110 def causation_id=(val) metadata[:causation_id] = val end
Sets causation_id
= in metadata. {railseventstore.org/docs/correlation_causation/ Find out more}
@param val [String] @return [String]
Source
# File lib/ruby_event_store/event.rb, line 120 def correlate_with(other_message) self.correlation_id = other_message.correlation_id || other_message.message_id self.causation_id = other_message.message_id self end
Sets correlation_id
and causation_id
in metadata based on correlation_id
and message_id
of the provided message. {railseventstore.org/docs/correlation_causation/ Find out more}
@param other_message [Event, command] message to correlate with. Most likely an event or a command. Must respond to correlation_id
and message_id. @return [String] set causation_id
Source
# File lib/ruby_event_store/event.rb, line 84 def correlation_id metadata[:correlation_id] end
Reads correlation_id
from metadata. {railseventstore.org/docs/correlation_causation/ Find out more}
@return [String, nil]
Source
# File lib/ruby_event_store/event.rb, line 93 def correlation_id=(val) metadata[:correlation_id] = val end
Sets correlation_id
in metadata. {railseventstore.org/docs/correlation_causation/ Find out more}
@param val [String] @return [String]
Source
# File lib/ruby_event_store/event.rb, line 32 def event_type metadata[:event_type] || self.class.name end
Type of event. Used when matching with subscribed handlers. @return [String]
Source
# File lib/ruby_event_store/event.rb, line 75 def hash # We don't use metadata because == does not use metadata [event_type, event_id, data].hash ^ self.class.hash end
Generates a Fixnum hash value for this object. This function have the property that a.eql?(b) implies a.hash == b.hash.
The hash value is used along with eql? by the Hash class to determine if two objects reference the same hash key.
This hash is based on
-
class
-
data
Source
# File lib/ruby_event_store/event.rb, line 26 def message_id event_id end
Event
id @return [String]
Source
# File lib/ruby_event_store/event.rb, line 39 def timestamp metadata[:timestamp] end
Timestamp from metadata
@return [Time, nil]
Source
# File lib/ruby_event_store/event.rb, line 46 def valid_at metadata[:valid_at] end
Validity time from metadata
@return [Time, nil]