Module: Messaging::Message::ClassMethods

Defined in:
lib/messaging/message.rb

Instance Method Summary collapse

Instance Method Details

#default_topic_nameObject



70
71
72
73
74
# File 'lib/messaging/message.rb', line 70

def default_topic_name
  return superclass.topic if superclass.respond_to?(:topic)

  message_name.gsub('/', '-')
end

#key_attribute(attribute = nil) ⇒ Object

The attribute which value should be used as the key of the message. Must specify an attribute if ordering is important.



65
66
67
68
# File 'lib/messaging/message.rb', line 65

def key_attribute(attribute = nil)
  @key_attribute = attribute if attribute
  @key_attribute
end

#message_nameObject



86
87
88
# File 'lib/messaging/message.rb', line 86

def message_name
  name.underscore
end

#message_typeObject



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

def message_type
  to_s
end

#publish(attributes) ⇒ Message

Shorcut for creating a new message and publishing it

Parameters:

  • attributes (Hash)

    The attributes of the message

Options Hash (attributes):

  • :expected_version (:any, Integer)

    Concurrency control

Returns:

  • (Message)

    the message just published

Raises:



82
83
84
# File 'lib/messaging/message.rb', line 82

def publish(attributes)
  new(attributes).publish
end

#stream_name(name = nil) ⇒ Object

The stream that the message will be stored in when published.

Stream names consists of the stream category and the stream id separated by a $. For instance “customer$123” where “customer” is the category and “123” is the id.

When no stream name is given the message will not be persisted in the message store.

Examples:

class CustomerRegistered
  include Messaging::Message

  stream_name -> { "customer$#{customer_id}"}

  attribute :customer_id, Integer
 end

 CustomerRegistered.new(customer_id: 123).stream_name
 # => "customer$123"

Parameters:

  • name (#call, String, nil) (defaults to: nil)

    The name of the stream, will be evaluated in the context of the message instance.



51
52
53
54
# File 'lib/messaging/message.rb', line 51

def stream_name(name = nil)
  return @stream_name unless name
  @stream_name = name
end

#topic(topic_name = nil) ⇒ Object

By default the topic is the same as the name of the message. We change the / that would be set for a namespaced message as “/” isn’t valid in a topic To change the topic for a message just set it to whatever you want in your class definition.



59
60
61
# File 'lib/messaging/message.rb', line 59

def topic(topic_name = nil)
  @topic ||= topic_name&.to_s || default_topic_name
end