Class: Synapse::Message

Inherits:
Object
  • Object
show all
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.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, metadata, payload) ⇒ undefined

Parameters:

  • id (String)
  • metadata (Hash)
  • payload (Object)


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

#idString (readonly)

Unique identifier of this message

Returns:

  • (String)


13
14
15
# File 'lib/synapse/common/message.rb', line 13

def id
  @id
end

#metadataHash (readonly)

Metadata attached to this message by the application

Returns:

  • (Hash)


17
18
19
# File 'lib/synapse/common/message.rb', line 17

def 
  @metadata
end

#payloadObject (readonly)

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

Returns:

  • (Object)


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

Yields:

Returns:

See Also:



74
75
76
# File 'lib/synapse/common/message.rb', line 74

def self.build(&block)
  builder.build(&block)
end

.builderClass

Returns the type of builder that can be used to build this type of message

Returns:

  • (Class)


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

Parameters:

  • additional_metadata (Hash)

Returns:



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_typeClass

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

Returns:

  • (Class)


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

Parameters:

  • replacement_metadata (Hash)

Returns:



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