lib/message_driver/broker.rb in message-driver-0.1.0 vs lib/message_driver/broker.rb in message-driver-0.2.0.rc1
- old
+ new
@@ -1,28 +1,25 @@
require 'forwardable'
+require 'logger'
module MessageDriver
class Broker
extend Forwardable
- attr_reader :adapter, :configuration, :destinations
+ attr_reader :adapter, :configuration, :destinations, :consumers, :logger
def_delegators :@adapter, :stop
class << self
def configure(options)
@instance = new(options)
end
- def method_missing(m, *args)
- @instance.send(m, *args)
+ def method_missing(m, *args, &block)
+ @instance.send(m, *args, &block)
end
- def with_transaction(options={}, &block)
- @instance.with_transaction(options, &block)
- end
-
def instance
@instance
end
def define
@@ -32,39 +29,41 @@
def initialize(options)
@adapter = resolve_adapter(options[:adapter], options)
@configuration = options
@destinations = {}
+ @consumers = {}
+ @logger = options[:logger] || Logger.new(STDOUT).tap{|l| l.level = Logger::INFO}
+ logger.debug "MessageDriver configured successfully!"
end
- def publish(destination, body, headers={}, properties={})
- dest = find_destination(destination)
- dest.publish(body, headers, properties)
- end
-
- def pop_message(destination, options={})
- dest = find_destination(destination)
- dest.pop_message(options)
- end
-
def dynamic_destination(dest_name, dest_options={}, message_props={})
- adapter.create_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 = dynamic_destination(dest_name, dest_options, message_props)
+ dest = Client.dynamic_destination(dest_name, dest_options, message_props)
@destinations[key] = dest
end
- def with_transaction(options={}, &block)
- adapter.with_transaction(options, &block)
+ def consumer(key, &block)
+ raise MessageDriver::Error, "you must provide a block" unless block_given?
+ @consumers[key] = block
end
- private
+ def find_destination(destination_name)
+ destination = @destinations[destination_name]
+ raise MessageDriver::NoSuchDestinationError, "no destination #{destination_name} has been configured" if destination.nil?
+ destination
+ end
- def find_destination(destination)
- @destinations[destination]
+ def find_consumer(consumer_name)
+ consumer = @consumers[consumer_name]
+ raise 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"