class BunnyExchanges::Manager
Class keeps all configured exchanges through the same connection and channel.
Attributes
config[R]
exchanges[R]
Public Class Methods
new(config = BunnyExchanges.configuration)
click to toggle source
Constructor.
@param [BunnyExchanges::Configuration] the exchanges configuration.
# File lib/bunny_exchanges/manager.rb, line 12 def initialize config = BunnyExchanges.configuration @config = config @exchanges = {} @channels = {} end
Public Instance Methods
get(action, connection_name)
click to toggle source
Gets or builds the required exchange.
@param [Symbol, Symbol] the action name and the connection name @return [Bunny::Exchange] the required bunny exchange. @raise [BunnyExchanges::Manager::UndefinedExchange] when the exchange is not defined.
# File lib/bunny_exchanges/manager.rb, line 23 def get action, connection_name exchanges[action.to_sym] ||= begin name, options = params_for(action) channel_for(connection_name).exchange(name, options) end end
Private Instance Methods
channel_for(connection_name)
click to toggle source
# File lib/bunny_exchanges/manager.rb, line 52 def channel_for connection_name @channels[connection_name] ||= begin conn = Bunny.new(config.connection_config(connection_name)) conn.start conn.create_channel end end
config_for(action)
click to toggle source
# File lib/bunny_exchanges/manager.rb, line 48 def config_for action config.exchanges.fetch(action.to_s) { raise_undefined(action) } end
options_for(action)
click to toggle source
# File lib/bunny_exchanges/manager.rb, line 42 def options_for action config_for(action).reduce({}) do |hash, (option, value)| hash.merge! option.to_sym => value end end
params_for(action)
click to toggle source
# File lib/bunny_exchanges/manager.rb, line 35 def params_for action options = options_for(action) name = options.delete(:name) [name, options] end
raise_undefined(action)
click to toggle source
# File lib/bunny_exchanges/manager.rb, line 61 def raise_undefined action raise BunnyExchanges::UndefinedExchange, "No exchange is defined for action #{action}." end