Class: Messaging::Adapters::Test::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/messaging/adapters/test/store.rb

Overview

Message store adapter used for testing. It stores all messages in memory. Prefer accessing the message store through Messaging.message_store instead of using it directly.

Examples:

Using Test as the default message store adapter during tests:

# Put this in an initializer
Messaging.setup do |config|
  config.message_store.adapter = Rails.env.test? ? :test : :postgres
end

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStore

Returns a new instance of Store.



36
37
38
39
# File 'lib/messaging/adapters/test/store.rb', line 36

def initialize
  @categories = Categories.new
  clear!
end

Instance Attribute Details

#categoriesObject (readonly)

Returns the value of attribute categories.



34
35
36
# File 'lib/messaging/adapters/test/store.rb', line 34

def categories
  @categories
end

#messagesArray<Messaging::Message> (readonly)

Access to all messages.

Examples:

Check that a message has been added to the store with Rspec

expect do
  # Your code that should add a message to the store
end.to change { Messaging.message_store.messages.count }.from(0).to(1)

Returns:



29
30
31
# File 'lib/messaging/adapters/test/store.rb', line 29

def messages
  @messages
end

#streamsStreams (readonly)

Returns all the streams in the store.

Returns:

  • (Streams)

    all the streams in the store

See Also:



32
33
34
# File 'lib/messaging/adapters/test/store.rb', line 32

def streams
  @streams
end

Instance Method Details

#call(message) ⇒ Messaging::Message

Writes the message to the store Skips messages that hasn’t defined a stream name

Parameters:

Returns:

  • (Messaging::Message)

    A new copy of the message with the stream position added (if persisted)



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/messaging/adapters/test/store.rb', line 72

def call(message)
  return message unless message.stream_name

  stream = stream(message.stream_name)
  category = category(message.stream_name.split('$').first)
  ExpectedVersion.new(message.expected_version || :any).match!(stream.current_position)
  persisted_message = message.class.new(message.attributes.merge(stream_position: stream.current_position + 1))
  @messages << persisted_message
  stream.messages << persisted_message
  category.messages << persisted_message
  persisted_message
end

#category(name) ⇒ Object



48
49
50
# File 'lib/messaging/adapters/test/store.rb', line 48

def category(name)
  categories[name]
end

#clear!Object



60
61
62
63
64
# File 'lib/messaging/adapters/test/store.rb', line 60

def clear!
  @streams = Hash.new { |h, k| h[k] = Stream.new(k) }
  categories.clear!
  @messages = []
end

#messages_in_streams(*streams) ⇒ Array<Messaging::Message>

Access to all messages in the given streams

Parameters:

  • streams (Array<String>)

    List of one or more streams to get messages from

Returns:



56
57
58
# File 'lib/messaging/adapters/test/store.rb', line 56

def messages_in_streams(*streams)
  messages.select { |m| streams.flatten.map(&:to_s).include? m.stream_name }
end

#stream(name) ⇒ Stream

Get a specific stream by name

Returns:

See Also:

  • Messaging.strea


44
45
46
# File 'lib/messaging/adapters/test/store.rb', line 44

def stream(name)
  streams[name]
end