Class: BunnyMock::Exchange
- Inherits:
-
Object
- Object
- BunnyMock::Exchange
- Defined in:
- lib/bunny_mock/exchange.rb
Direct Known Subclasses
BunnyMock::Exchanges::Direct, BunnyMock::Exchanges::Fanout, BunnyMock::Exchanges::Header, BunnyMock::Exchanges::Topic
Instance Attribute Summary collapse
-
#arguments ⇒ Hash
readonly
Any additional declaration arguments.
-
#auto_delete ⇒ Boolean
(also: #auto_delete?)
readonly
If the exchange was declared with auto deletion.
-
#channel ⇒ BunnyMock::Channel
readonly
Channel used by exchange.
-
#durable ⇒ Boolean
(also: #durable?)
readonly
If the exchange was declared as durable.
-
#internal ⇒ Boolean
(also: #internal?)
readonly
If the exchange was declared as internal.
-
#name ⇒ String
readonly
Exchange name.
-
#opts ⇒ Hash
readonly
Creation options.
-
#type ⇒ String
readonly
Exchange type.
Bunny API collapse
-
#bind(exchange, opts = {}) ⇒ BunnyMock::Exchange
Bind this exchange to another exchange.
-
#delete ⇒ Object
Delete this exchange.
-
#publish(payload, opts = {}) ⇒ BunnyMock::Exchange
Publish a message.
-
#unbind(exchange, opts = {}) ⇒ BunnyMock::Exchange
Unbind this exchange from another exchange.
Class Method Summary collapse
-
.declare(channel, name = '', opts = {}) ⇒ BunnyMock::Exchange
Create a new Exchange instance.
Instance Method Summary collapse
-
#bound_to?(exchange, opts = {}) ⇒ Boolean
Check if this exchange is bound to another exchange.
-
#deliver(payload, opts, key) ⇒ Object
Deliver a message to routes.
-
#has_binding?(exchange_or_queue, opts = {}) ⇒ Boolean
rubocop:disable Style/PredicateName.
-
#routes_to?(exchange_or_queue, opts = {}) ⇒ Boolean
Check if a queue is bound to this exchange.
Instance Attribute Details
#arguments ⇒ Hash (readonly)
Returns Any additional declaration arguments.
61 62 63 |
# File 'lib/bunny_mock/exchange.rb', line 61 def arguments @arguments end |
#auto_delete ⇒ Boolean (readonly) Also known as: auto_delete?
Returns If the exchange was declared with auto deletion.
53 54 55 |
# File 'lib/bunny_mock/exchange.rb', line 53 def auto_delete @auto_delete end |
#channel ⇒ BunnyMock::Channel (readonly)
Returns Channel used by exchange.
37 38 39 |
# File 'lib/bunny_mock/exchange.rb', line 37 def channel @channel end |
#durable ⇒ Boolean (readonly) Also known as: durable?
Returns If the exchange was declared as durable.
49 50 51 |
# File 'lib/bunny_mock/exchange.rb', line 49 def durable @durable end |
#internal ⇒ Boolean (readonly) Also known as: internal?
Returns If the exchange was declared as internal.
57 58 59 |
# File 'lib/bunny_mock/exchange.rb', line 57 def internal @internal end |
#name ⇒ String (readonly)
Returns Exchange name.
40 41 42 |
# File 'lib/bunny_mock/exchange.rb', line 40 def name @name end |
#opts ⇒ Hash (readonly)
Returns Creation options.
46 47 48 |
# File 'lib/bunny_mock/exchange.rb', line 46 def opts @opts end |
#type ⇒ String (readonly)
Returns Exchange type.
43 44 45 |
# File 'lib/bunny_mock/exchange.rb', line 43 def type @type end |
Class Method Details
.declare(channel, name = '', opts = {}) ⇒ BunnyMock::Exchange
Create a new BunnyMock::Exchange instance
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/bunny_mock/exchange.rb', line 20 def declare(channel, name = '', opts = {}) # get requested type type = opts.fetch :type, :direct # get needed class type klazz = BunnyMock::Exchanges.const_get type.to_s.capitalize # create exchange of desired type klazz.new channel, name, type, opts end |
Instance Method Details
#bind(exchange, opts = {}) ⇒ BunnyMock::Exchange
Bind this exchange to another exchange
144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/bunny_mock/exchange.rb', line 144 def bind(exchange, opts = {}) if exchange.respond_to?(:add_route) # we can do the binding ourselves exchange.add_route opts.fetch(:routing_key, @name), self else # we need the channel to look up the exchange @channel.xchg_bind self, opts.fetch(:routing_key, @name), exchange end self end |
#bound_to?(exchange, opts = {}) ⇒ Boolean
Check if this exchange is bound to another exchange
194 195 196 197 198 199 200 201 202 203 |
# File 'lib/bunny_mock/exchange.rb', line 194 def bound_to?(exchange, opts = {}) if exchange.respond_to?(:routes_to?) # we can find out on the exchange object exchange.routes_to? self, opts else # we need the channel to look up the exchange @channel.xchg_bound_to? self, opts.fetch(:routing_key, @name), exchange end end |
#delete ⇒ Object
Delete this exchange
127 128 129 130 131 |
# File 'lib/bunny_mock/exchange.rb', line 127 def delete(*) @channel.deregister_exchange self @deleted = true end |
#deliver(payload, opts, key) ⇒ Object
Deliver a message to routes
236 237 238 |
# File 'lib/bunny_mock/exchange.rb', line 236 def deliver(payload, opts, key) # noOp end |
#has_binding?(exchange_or_queue, opts = {}) ⇒ Boolean
rubocop:disable Style/PredicateName
222 223 224 225 |
# File 'lib/bunny_mock/exchange.rb', line 222 def has_binding?(exchange_or_queue, opts = {}) # rubocop:disable Style/PredicateName warn '[DEPRECATED] `has_binding?` is deprecated. Please use `routes_to?` instead.' routes_to?(exchange_or_queue, opts) end |
#publish(payload, opts = {}) ⇒ BunnyMock::Exchange
Publish a message
115 116 117 118 119 120 |
# File 'lib/bunny_mock/exchange.rb', line 115 def publish(payload, opts = {}) # handle message sending, varies by type deliver(payload, opts.merge(exchange: name), opts.fetch(:routing_key, '')) self end |
#routes_to?(exchange_or_queue, opts = {}) ⇒ Boolean
Check if a queue is bound to this exchange
216 217 218 219 220 |
# File 'lib/bunny_mock/exchange.rb', line 216 def routes_to?(exchange_or_queue, opts = {}) route = exchange_or_queue.respond_to?(:name) ? exchange_or_queue.name : exchange_or_queue rk = opts.fetch(:routing_key, route) @routes.key?(rk) && @routes[rk].any? { |r| r == exchange_or_queue } end |
#unbind(exchange, opts = {}) ⇒ BunnyMock::Exchange
Unbind this exchange from another exchange
169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/bunny_mock/exchange.rb', line 169 def unbind(exchange, opts = {}) if exchange.respond_to?(:remove_route) # we can do the unbinding ourselves exchange.remove_route opts.fetch(:routing_key, @name), self else # we need the channel to look up the exchange @channel.xchg_unbind opts.fetch(:routing_key, @name), exchange, self end self end |