lib/messaging/message.rb in messaging-3.5.4 vs lib/messaging/message.rb in messaging-3.5.5
- old
+ new
@@ -23,10 +23,36 @@
attribute :timestamp, Time, default: ->(*) { Time.current.utc }
attribute :uuid, String, default: ->(*) { SecureRandom.uuid }
end
module ClassMethods
+ # 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.
+ #
+ # @param [#call,String,nil] name The name of the stream, will be evaluated in the context
+ # of the message instance.
+ #
+ # @example
+ # class CustomerRegistered
+ # include Messaging::Message
+ #
+ # stream_name -> { "customer$#{customer_id}"}
+ #
+ # attribute :customer_id, Integer
+ # end
+ #
+ # CustomerRegistered.new(customer_id: 123).stream_name
+ # # => "customer$123"
+ def stream_name(name = nil)
+ return @stream_name unless name
+ @stream_name = name
+ end
+
# 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.
def topic(topic_name = nil)
@topic ||= topic_name&.to_s || default_topic_name
@@ -86,11 +112,11 @@
def topic
self.class.topic
end
def stream_name
- # define stream_name in your message class to override
- nil
+ return unless self.class.stream_name
+ instance_exec(&self.class.stream_name)
end
def stream_category
return unless stream_name