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.
Class Method Summary collapse
-
.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) ⇒ 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) ⇒ undefined
29 30 31 32 33 34 35 |
# File 'lib/synapse/common/message.rb', line 29 def initialize(id, , payload) @id = id @metadata = @payload = payload @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 |
Class Method Details
.build {|MessageBuilder| ... } ⇒ Message
Yields a message builder that can be used to produce a message
74 75 76 |
# File 'lib/synapse/common/message.rb', line 74 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
80 81 82 |
# File 'lib/synapse/common/message.rb', line 80 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
49 50 51 52 53 54 55 |
# File 'lib/synapse/common/message.rb', line 49 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.
41 42 43 |
# File 'lib/synapse/common/message.rb', line 41 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
61 62 63 64 65 66 67 |
# File 'lib/synapse/common/message.rb', line 61 def () return self if @metadata == builder = self.class.builder.new build_duplicate builder, builder.build end |