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



29
30
31
32
33
34
35
# File 'lib/synapse/common/message.rb', line 29

def initialize(id, , payload)
  @id = id
   = 
  @payload = payload

  .freeze
end

Instance Attribute Details

#idString (readonly)

Unique identifier of this message



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



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

def 
  
end

#payloadObject (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

.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.



75
76
77
78
79
80
81
82
83
# File 'lib/synapse/common/message.rb', line 75

def self.as_message(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



90
91
92
# File 'lib/synapse/common/message.rb', line 90

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

.builderClass

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



96
97
98
# File 'lib/synapse/common/message.rb', line 96

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, .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.



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  == 

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