Class: Synapse::Message
- Inherits:
-
Object
- Object
- Synapse::Message
- Defined in:
- lib/synapse/common/message.rb
Overview
Representation of a message containing a payload and metadata
Instead of using this class directly, it is recommended to use a subclass specifically for commands, events or domain events.
Two messages with the same identifier should be interpreted as different representations of the same conceptual message. In such case, the metadata may be different for both representations. The payload may be identical.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#id ⇒ String
readonly
Unique identifier of this message.
-
#metadata ⇒ Hash
readonly
Metadata attached to this message by the application.
-
#payload ⇒ Object
readonly
Payload of this message; examples include commands and events.
-
#timestamp ⇒ Time
readonly
Timestamp recorded when the message was created.
Class Method Summary collapse
-
.as_message(object) ⇒ Message
Wraps an object into a message as its payload.
-
.build {|MessageBuilder| ... } ⇒ Message
Yields a message builder that can be used to produce a message.
-
.builder ⇒ Class
Returns the type of builder that can be used to build this type of message.
Instance Method Summary collapse
-
#and_metadata(additional_metadata) ⇒ Message
Returns a copy of this message with the given metadata merged in.
- #initialize(id, metadata, payload, timestamp) ⇒ undefined constructor
-
#payload_type ⇒ Class
Returns the class of the payload of this message; use this instead of calling payload and class, in case of lazily deserializing messages.
-
#with_metadata(replacement_metadata) ⇒ Message
Returns a copy of this message with the metadata replaced with the given metadata.
Constructor Details
#initialize(id, metadata, payload, timestamp) ⇒ undefined
34 35 36 37 38 39 40 41 |
# File 'lib/synapse/common/message.rb', line 34 def initialize(id, , payload, ) @id = id @metadata = @payload = payload @timestamp = @metadata.freeze end |
Instance Attribute Details
#id ⇒ String (readonly)
Unique identifier of this message
13 14 15 |
# File 'lib/synapse/common/message.rb', line 13 def id @id end |
#metadata ⇒ Hash (readonly)
Metadata attached to this message by the application
17 18 19 |
# File 'lib/synapse/common/message.rb', line 17 def @metadata end |
#payload ⇒ Object (readonly)
Payload of this message; examples include commands and events. A payload is expected to be immutable to provide thread safety.
23 24 25 |
# File 'lib/synapse/common/message.rb', line 23 def payload @payload end |
#timestamp ⇒ Time (readonly)
Timestamp recorded when the message was created
27 28 29 |
# File 'lib/synapse/common/message.rb', line 27 def @timestamp end |
Class Method Details
.as_message(object) ⇒ Message
Wraps an object into a message as its payload
If the given object is an message, it will be returned unchanged.
81 82 83 84 85 86 87 88 89 |
# File 'lib/synapse/common/message.rb', line 81 def self.(object) unless object.is_a? Message object = self.build do |builder| builder.payload = object end end object end |
.build {|MessageBuilder| ... } ⇒ Message
Yields a message builder that can be used to produce a message
96 97 98 |
# File 'lib/synapse/common/message.rb', line 96 def self.build(&block) builder.build &block end |
.builder ⇒ Class
Returns the type of builder that can be used to build this type of message
102 103 104 |
# File 'lib/synapse/common/message.rb', line 102 def self.builder MessageBuilder end |
Instance Method Details
#and_metadata(additional_metadata) ⇒ Message
Returns a copy of this message with the given metadata merged in
55 56 57 58 59 60 61 |
# File 'lib/synapse/common/message.rb', line 55 def () return self if .empty? builder = self.class.builder.new build_duplicate builder, @metadata.merge() builder.build end |
#payload_type ⇒ Class
Returns the class of the payload of this message; use this instead of calling payload and class, in case of lazily deserializing messages.
47 48 49 |
# File 'lib/synapse/common/message.rb', line 47 def payload_type @payload.class end |
#with_metadata(replacement_metadata) ⇒ Message
Returns a copy of this message with the metadata replaced with the given metadata
67 68 69 70 71 72 73 |
# File 'lib/synapse/common/message.rb', line 67 def () return self if @metadata == builder = self.class.builder.new build_duplicate builder, builder.build end |