Class: RubyEventStore::Event
- Inherits:
-
Object
- Object
- RubyEventStore::Event
- Defined in:
- lib/ruby_event_store/event.rb
Overview
Data structure representing an event
Constant Summary collapse
- BIG_VALUE =
0b111111100100000010010010110011101011000101010101001100100110000
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#event_id ⇒ Object
readonly
Returns the value of attribute event_id.
-
#metadata ⇒ Object
readonly
Returns the value of attribute metadata.
Instance Method Summary collapse
-
#==(other_event) ⇒ TrueClass, FalseClass
(also: #eql?)
Two events are equal if: * they are of the same class * have identical event id * have identical data (verified with eql? method).
-
#causation_id ⇒ String?
Reads causation_id from metadata.
-
#causation_id=(val) ⇒ String
Sets causation_id= in metadata.
-
#correlate_with(other_message) ⇒ String
Sets correlation_id and causation_id in metadata based on correlation_id and message_id of the provided message.
-
#correlation_id ⇒ String?
Reads correlation_id from metadata.
-
#correlation_id=(val) ⇒ String
Sets correlation_id in metadata.
-
#event_type ⇒ String
Type of event.
-
#hash ⇒ Object
Generates a Fixnum hash value for this object.
-
#initialize(event_id: SecureRandom.uuid, metadata: nil, data: {}) ⇒ Event
constructor
Instantiates a new event.
-
#message_id ⇒ String
Event id.
-
#timestamp ⇒ Time?
Timestamp from metadata.
-
#valid_at ⇒ Time?
Validity time from metadata.
Constructor Details
#initialize(event_id: SecureRandom.uuid, metadata: nil, data: {}) ⇒ Event
Instantiates a new event
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
#data ⇒ Object (readonly)
Returns the value of attribute data.
23 24 25 |
# File 'lib/ruby_event_store/event.rb', line 23 def data @data end |
#event_id ⇒ Object (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 |
#metadata ⇒ Object (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 id
-
have identical data (verified with eql? method)
Event equality ignores metadata!
60 61 62 63 64 |
# File 'lib/ruby_event_store/event.rb', line 60 def ==(other_event) other_event.instance_of?(self.class) && other_event.event_id.eql?(event_id) && other_event.data.eql?(data) end |
#causation_id ⇒ String?
Reads causation_id from metadata. / Find out more
109 110 111 |
# File 'lib/ruby_event_store/event.rb', line 109 def causation_id [:causation_id] end |
#causation_id=(val) ⇒ String
Sets causation_id= in metadata. / Find out more
118 119 120 |
# File 'lib/ruby_event_store/event.rb', line 118 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
128 129 130 131 132 |
# File 'lib/ruby_event_store/event.rb', line 128 def correlate_with() self.correlation_id = .correlation_id || . self.causation_id = . self end |
#correlation_id ⇒ String?
Reads correlation_id from metadata. / Find out more
92 93 94 |
# File 'lib/ruby_event_store/event.rb', line 92 def correlation_id [:correlation_id] end |
#correlation_id=(val) ⇒ String
Sets correlation_id in metadata. / Find out more
101 102 103 |
# File 'lib/ruby_event_store/event.rb', line 101 def correlation_id=(val) [:correlation_id] = val end |
#event_type ⇒ String
Type of event. Used when matching with subscribed handlers.
33 34 35 |
# File 'lib/ruby_event_store/event.rb', line 33 def event_type self.class.name end |
#hash ⇒ Object
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
79 80 81 82 83 84 85 86 |
# File 'lib/ruby_event_store/event.rb', line 79 def hash # We don't use metadata because == does not use metadata [ self.class, event_id, data ].hash ^ BIG_VALUE end |
#message_id ⇒ String
Event id
27 28 29 |
# File 'lib/ruby_event_store/event.rb', line 27 def event_id end |
#timestamp ⇒ Time?
Timestamp from metadata
40 41 42 |
# File 'lib/ruby_event_store/event.rb', line 40 def [:timestamp] end |
#valid_at ⇒ Time?
Validity time from metadata
47 48 49 |
# File 'lib/ruby_event_store/event.rb', line 47 def valid_at [:valid_at] end |