lib/message_driver/broker.rb in message-driver-0.4.0 vs lib/message_driver/broker.rb in message-driver-0.5.0

- old
+ new

@@ -7,11 +7,11 @@ attr_reader :adapter, :configuration, :destinations, :consumers, :name class << self def configure(name = DEFAULT_BROKER_NAME, options) if brokers.keys.include? name - raise BrokerAlreadyConfigured, "there is already a broker named #{name} configured" + fail BrokerAlreadyConfigured, "there is already a broker named #{name} configured" end brokers[name] = new(name, options) end def define(name = DEFAULT_BROKER_NAME) @@ -19,11 +19,12 @@ end def broker(name = DEFAULT_BROKER_NAME) result = brokers[name] if result.nil? - raise BrokerNotConfigured, "There is no broker named #{name} configured. The configured brokers are #{brokers.keys}" + fail BrokerNotConfigured, + "There is no broker named #{name} configured. The configured brokers are #{brokers.keys}" end result end def client(name) @@ -63,15 +64,15 @@ end private def brokers - @brokers ||= { } + @brokers ||= {} end def clients - @clients ||= { } + @clients ||= {} end def each_broker brokers.keys.each do |k| yield brokers[k] @@ -105,66 +106,70 @@ def stopped? @stopped end def restart - unless stopped? - @adapter.stop - end + @adapter.stop unless stopped? @adapter = resolve_adapter(@configuration[:adapter], @configuration) @stopped = false end - def dynamic_destination(dest_name, dest_options={}, message_props={}) + def dynamic_destination(dest_name, dest_options = {}, message_props = {}) client.dynamic_destination(dest_name, dest_options, message_props) end - def destination(key, dest_name, dest_options={}, message_props={}) - dest = self.dynamic_destination(dest_name, dest_options, message_props) + def destination(key, dest_name, dest_options = {}, message_props = {}) + dest = dynamic_destination(dest_name, dest_options, message_props) @destinations[key] = dest end def consumer(key, &block) - raise MessageDriver::Error, 'you must provide a block' unless block_given? + fail MessageDriver::Error, 'you must provide a block' unless block_given? @consumers[key] = block end def find_destination(destination_name) destination = @destinations[destination_name] - raise MessageDriver::NoSuchDestinationError, "no destination #{destination_name} has been configured" if destination.nil? + if destination.nil? + fail MessageDriver::NoSuchDestinationError, "no destination #{destination_name} has been configured" + end destination end def find_consumer(consumer_name) consumer = @consumers[consumer_name] - raise MessageDriver::NoSuchConsumerError, "no consumer #{consumer_name} has been configured" if consumer.nil? + fail MessageDriver::NoSuchConsumerError, "no consumer #{consumer_name} has been configured" if consumer.nil? consumer end private def resolve_adapter(adapter, options) case adapter when nil - raise 'you must specify an adapter' + fail 'you must specify an adapter' when Symbol, String resolve_adapter(find_adapter_class(adapter), options) when Class resolve_adapter(adapter.new(self, options), options) when MessageDriver::Adapters::Base adapter else - raise "adapter must be a MessageDriver::Adapters::Base, but this object is a #{adapter.class}" + fail "adapter must be a MessageDriver::Adapters::Base, but this object is a #{adapter.class}" end end def find_adapter_class(adapter_name) require "message_driver/adapters/#{adapter_name}_adapter" adapter_method = "#{adapter_name}_adapter" unless respond_to?(adapter_method) - raise "the adapter #{adapter_name} must provide MessageDriver::Broker##{adapter_method} that returns the adapter class" + fail ['the adapter', + adapter_name, + 'must provide', + "MessageDriver::Broker##{adapter_method}", + 'that returns the adapter class'].join(' ') end send(adapter_method) end end