Class: Lepus::Testing::Exchange
- Inherits:
-
Object
- Object
- Lepus::Testing::Exchange
- Defined in:
- lib/lepus/testing/exchange.rb
Overview
Represents a fake exchange that stores published messages for testing
Instance Attribute Summary collapse
-
#messages ⇒ Object
readonly
Returns the value of attribute messages.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
-
.[](name) ⇒ Lepus::Testing::Exchange
Get or create an exchange by name.
-
.all ⇒ Hash<String, Lepus::Testing::Exchange>
Get all exchanges.
-
.clear_all! ⇒ Object
Clear all exchanges (remove them completely).
-
.clear_all_messages! ⇒ Object
Clear all messages from all exchanges.
-
.total_messages ⇒ Object
Get the total number of messages across all exchanges.
Instance Method Summary collapse
-
#add_message(message) ⇒ Object
Add a message to this exchange.
-
#clear_messages ⇒ Object
Clear all messages from this exchange.
-
#empty? ⇒ Boolean
Check if this exchange has any messages.
-
#find_messages(criteria = {}) ⇒ Array<Hash>
Find messages matching specific criteria.
-
#initialize(name) ⇒ Exchange
constructor
A new instance of Exchange.
-
#size ⇒ Object
Get the number of messages in this exchange.
Constructor Details
#initialize(name) ⇒ Exchange
Returns a new instance of Exchange.
9 10 11 12 |
# File 'lib/lepus/testing/exchange.rb', line 9 def initialize(name) @name = name @messages = [] end |
Instance Attribute Details
#messages ⇒ Object (readonly)
Returns the value of attribute messages.
7 8 9 |
# File 'lib/lepus/testing/exchange.rb', line 7 def @messages end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
7 8 9 |
# File 'lib/lepus/testing/exchange.rb', line 7 def name @name end |
Class Method Details
.[](name) ⇒ Lepus::Testing::Exchange
Get or create an exchange by name
62 63 64 |
# File 'lib/lepus/testing/exchange.rb', line 62 def [](name) exchanges[name.to_s] ||= new(name.to_s) end |
.all ⇒ Hash<String, Lepus::Testing::Exchange>
Get all exchanges
68 69 70 |
# File 'lib/lepus/testing/exchange.rb', line 68 def all exchanges end |
.clear_all! ⇒ Object
Clear all exchanges (remove them completely)
78 79 80 |
# File 'lib/lepus/testing/exchange.rb', line 78 def clear_all! exchanges.clear end |
.clear_all_messages! ⇒ Object
Clear all messages from all exchanges
73 74 75 |
# File 'lib/lepus/testing/exchange.rb', line 73 def exchanges.each_value(&:clear_messages) end |
.total_messages ⇒ Object
Get the total number of messages across all exchanges
83 84 85 |
# File 'lib/lepus/testing/exchange.rb', line 83 def exchanges.values.sum(&:size) end |
Instance Method Details
#add_message(message) ⇒ Object
Add a message to this exchange
16 17 18 |
# File 'lib/lepus/testing/exchange.rb', line 16 def () @messages << end |
#clear_messages ⇒ Object
Clear all messages from this exchange
21 22 23 |
# File 'lib/lepus/testing/exchange.rb', line 21 def @messages.clear end |
#empty? ⇒ Boolean
Check if this exchange has any messages
31 32 33 |
# File 'lib/lepus/testing/exchange.rb', line 31 def empty? @messages.empty? end |
#find_messages(criteria = {}) ⇒ Array<Hash>
Find messages matching specific criteria
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/lepus/testing/exchange.rb', line 38 def (criteria = {}) return @messages if criteria.empty? @messages.select do || criteria.all? do |key, value| case key when :routing_key [:routing_key] == value when :payload [:payload] == value when :headers [:headers]&.any? { |k, v| value.any? { |vk, vv| k == vk && v == vv } } else [key] == value end end end end |
#size ⇒ Object
Get the number of messages in this exchange
26 27 28 |
# File 'lib/lepus/testing/exchange.rb', line 26 def size @messages.size end |