class Synapse::Message

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.

Attributes

id[R]

Unique identifier of this message @return [String]

metadata[R]

Metadata attached to this message by the application @return [Hash]

payload[R]

Payload of this message; examples include commands and events. A payload is expected to be immutable to provide thread safety.

@return [Object]

timestamp[R]

Timestamp recorded when the message was created @return [Time]

Public Class Methods

as_message(object) click to toggle source

Wraps an object into a message as its payload

If the given object is an message, it will be returned unchanged.

@param [Object] object @return [Message]

# File lib/synapse/common/message.rb, line 81
def self.as_message(object)
  unless object.is_a? Message
    object = self.build do |builder|
      builder.payload = object
    end
  end

  object
end
build(&block) click to toggle source

Yields a message builder that can be used to produce a message

@see MessageBuilder#build @yield [MessageBuilder] @return [Message]

# File lib/synapse/common/message.rb, line 96
def self.build(&block)
  builder.build &block
end
builder() click to toggle source

Returns the type of builder that can be used to build this type of message @return [Class]

# File lib/synapse/common/message.rb, line 102
def self.builder
  MessageBuilder
end
new(id, metadata, payload, timestamp) click to toggle source

@param [String] id @param [Hash] metadata @param [Object] payload @param [Time] timestamp @return [undefined]

# File lib/synapse/common/message.rb, line 34
def initialize(id, metadata, payload, timestamp)
  @id = id
  @metadata = metadata
  @payload = payload
  @timestamp = timestamp

  @metadata.freeze
end

Public Instance Methods

and_metadata(additional_metadata) click to toggle source

Returns a copy of this message with the given metadata merged in

@param [Hash] additional_metadata @return [Message]

# File lib/synapse/common/message.rb, line 55
def and_metadata(additional_metadata)
  return self if additional_metadata.empty?

  builder = self.class.builder.new
  build_duplicate builder, @metadata.merge(additional_metadata)
  builder.build
end
payload_type() click to toggle source

Returns the class of the payload of this message; use this instead of calling payload and class, in case of lazily deserializing messages.

@return [Class]

# File lib/synapse/common/message.rb, line 47
def payload_type
  @payload.class
end
with_metadata(replacement_metadata) click to toggle source

Returns a copy of this message with the metadata replaced with the given metadata

@param [Hash] replacement_metadata @return [Message]

# File lib/synapse/common/message.rb, line 67
def with_metadata(replacement_metadata)
  return self if @metadata == replacement_metadata

  builder = self.class.builder.new
  build_duplicate builder, replacement_metadata
  builder.build
end

Protected Instance Methods

build_duplicate(builder, metadata) click to toggle source

Populates a duplicated message with attributes from this message

@param [MessageBuilder] builder @param [Hash] metadata @return [undefined]

# File lib/synapse/common/message.rb, line 113
def build_duplicate(builder, metadata)
  builder.id = @id
  builder.metadata = metadata
  builder.payload = @payload
  builder.timestamp = @timestamp
end