class RubyEventStore::Event

Data structure representing an event

Attributes

data[R]
event_id[R]
metadata[R]

Public Class Methods

new(event_id: SecureRandom.uuid, metadata: nil, data: {}) click to toggle source

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]

# 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

Public Instance Methods

==(other_event) click to toggle source

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]

# 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
Also aliased as: eql?
causation_id() click to toggle source

Reads causation_id from metadata. {railseventstore.org/docs/correlation_causation/ Find out more}

@return [String, nil]

# File lib/ruby_event_store/event.rb, line 101
def causation_id
  metadata[:causation_id]
end
causation_id=(val) click to toggle source

Sets causation_id= in metadata. {railseventstore.org/docs/correlation_causation/ Find out more}

@param val [String] @return [String]

# File lib/ruby_event_store/event.rb, line 110
def causation_id=(val)
  metadata[:causation_id] = val
end
correlate_with(other_message) click to toggle source

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

# 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
correlation_id() click to toggle source

Reads correlation_id from metadata. {railseventstore.org/docs/correlation_causation/ Find out more}

@return [String, nil]

# File lib/ruby_event_store/event.rb, line 84
def correlation_id
  metadata[:correlation_id]
end
correlation_id=(val) click to toggle source

Sets correlation_id in metadata. {railseventstore.org/docs/correlation_causation/ Find out more}

@param val [String] @return [String]

# File lib/ruby_event_store/event.rb, line 93
def correlation_id=(val)
  metadata[:correlation_id] = val
end
eql?(other_event)
Alias for: ==
event_type() click to toggle source

Type of event. Used when matching with subscribed handlers. @return [String]

# File lib/ruby_event_store/event.rb, line 32
def event_type
  metadata[:event_type] || self.class.name
end
hash() click to toggle source

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

# 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
message_id() click to toggle source

Event id @return [String]

# File lib/ruby_event_store/event.rb, line 26
def message_id
  event_id
end
timestamp() click to toggle source

Timestamp from metadata

@return [Time, nil]

# File lib/ruby_event_store/event.rb, line 39
def timestamp
  metadata[:timestamp]
end
valid_at() click to toggle source

Validity time from metadata

@return [Time, nil]

# File lib/ruby_event_store/event.rb, line 46
def valid_at
  metadata[:valid_at]
end