Class: RubyEventStore::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_event_store/event.rb

Overview

Data structure representing an event

Constant Summary collapse

BIG_VALUE =
0b111111100100000010010010110011101011000101010101001100100110000

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(event_id: SecureRandom.uuid, metadata: nil, data: {}) ⇒ Event

Instantiates a new event

Parameters:

  • event_id (String) (defaults to: SecureRandom.uuid)

    event id

  • data (Hash) (defaults to: {})

    event data which belong to your application domain

  • metadata (Hash) (defaults to: nil)

    event metadata which are technical and not part of your domain such as remote_ip, request_id, correlation_id, causation_id etc.


17
18
19
20
21
# File 'lib/ruby_event_store/event.rb', line 17

def initialize(event_id: SecureRandom.uuid, metadata: nil, data: {})
  @event_id = event_id.to_s
  @metadata = Metadata.new(.to_h)
  @data     = data
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.


23
24
25
# File 'lib/ruby_event_store/event.rb', line 23

def data
  @data
end

#event_idObject (readonly)

Returns the value of attribute event_id.


23
24
25
# File 'lib/ruby_event_store/event.rb', line 23

def event_id
  @event_id
end

#metadataObject (readonly)

Returns the value of attribute metadata.


23
24
25
# File 'lib/ruby_event_store/event.rb', line 23

def 
  @metadata
end

Instance Method Details

#==(other_event) ⇒ TrueClass, FalseClass Also known as: eql?

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)

Event equality ignores metadata!

Parameters:

  • other_event (Event, Object)

    object to compare

Returns:

  • (TrueClass, FalseClass)

61
62
63
64
65
66
# File 'lib/ruby_event_store/event.rb', line 61

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

#causation_idString?

Reads causation_id from metadata. / Find out more

Returns:

  • (String, nil)

112
113
114
# File 'lib/ruby_event_store/event.rb', line 112

def causation_id
  [:causation_id]
end

#causation_id=(val) ⇒ String

Sets causation_id= in metadata. / Find out more

Parameters:

  • val (String)

Returns:

  • (String)

121
122
123
# File 'lib/ruby_event_store/event.rb', line 121

def causation_id=(val)
  [:causation_id]= val
end

#correlate_with(other_message) ⇒ String

Sets correlation_id and causation_id in metadata based on correlation_id and message_id of the provided message. / Find out more

Parameters:

  • other_message (Event, command)

    message to correlate with. Most likely an event or a command. Must respond to correlation_id and message_id.

Returns:

  • (String)

    set causation_id


131
132
133
134
135
# File 'lib/ruby_event_store/event.rb', line 131

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_idString?

Reads correlation_id from metadata. / Find out more

Returns:

  • (String, nil)

95
96
97
# File 'lib/ruby_event_store/event.rb', line 95

def correlation_id
  [:correlation_id]
end

#correlation_id=(val) ⇒ String

Sets correlation_id in metadata. / Find out more

Parameters:

  • val (String)

Returns:

  • (String)

104
105
106
# File 'lib/ruby_event_store/event.rb', line 104

def correlation_id=(val)
  [:correlation_id] = val
end

#event_typeString

Type of event. Used when matching with subscribed handlers.

Returns:

  • (String)

33
34
35
# File 'lib/ruby_event_store/event.rb', line 33

def event_type
  [:event_type] || self.class.name
end

#hashObject

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

  • event_id

  • data


81
82
83
84
85
86
87
88
89
# File 'lib/ruby_event_store/event.rb', line 81

def hash
  # We don't use metadata because == does not use metadata
  [
    self.class,
    event_type,
    event_id,
    data
  ].hash ^ BIG_VALUE
end

#message_idString

Event id

Returns:

  • (String)

27
28
29
# File 'lib/ruby_event_store/event.rb', line 27

def message_id
  event_id
end

#timestampTime?

Timestamp from metadata

Returns:

  • (Time, nil)

40
41
42
# File 'lib/ruby_event_store/event.rb', line 40

def timestamp
  [:timestamp]
end

#valid_atTime?

Validity time from metadata

Returns:

  • (Time, nil)

47
48
49
# File 'lib/ruby_event_store/event.rb', line 47

def valid_at
  [:valid_at]
end