module Messaging module Adapters class Postgres class Stream # @return [String] the name of the stream attr_reader :name # @return [String] the stream category attr_reader :category # @return [String] the stream id attr_reader :id # Should not be used directly. # Use {Messaging.stream} or {Store#stream} # @api private # @see Messaging.stream # @see Store.stream def initialize(name) @name = name @category, @id = name.split('$') end # Access to all messages in the stream sorted by stream_position # @return [ActiveRecord::Relation] def messages SerializedMessage.where(stream_category: category, stream_id: id).order(:stream_position) end # The current position of the last message in the stream # @return [-1] if the stream is empty -1 will be returned # @return [Integer] the stream position of the last message in the stream def current_position messages.maximum(:stream_position) || -1 end def to_s name end def inspect info = "current_position: #{current_position}" "# #{info}>" end end end end end