Sha256: 213fb5c45ffe010ab280e16b4859b2251dcdd2c8ec935ebc819cbc4fc069a667

Contents?: true

Size: 1.06 KB

Versions: 2

Compression:

Stored size: 1.06 KB

Contents

require 'rosetta_queue/adapters/base'

module RosettaQueue
  class Adapter

    class << self
      attr_writer :user, :password, :host, :port, :options

      def define
        yield self
      end

      def reset
        @user, @password, @host, @port, @options, @adapter_class = nil, nil, nil, nil, nil, nil
      end

      def type=(adapter_prefix)
        require "rosetta_queue/adapters/#{adapter_prefix}"
        @adapter_class = RosettaQueue::Gateway.const_get("#{adapter_prefix.to_s.classify}Adapter")

        rescue LoadError
          raise AdapterException, "Adapter type '#{adapter_prefix}' does not match existing adapters!"
      end

      def instance
        raise AdapterException, "Adapter type was never defined!" unless @adapter_class
        @adapter_class.new({:user => @user, :password => @password, :host => @host, :port => @port, :opts => opts})
      end

      private

      def opts
        raise AdapterException, "Adapter options should be a hash" unless @options.nil? || @options.is_a?(Hash)
        @options ||= {}
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
bmabey-rosetta_queue-0.3.3 lib/rosetta_queue/adapter.rb
rosetta_queue-0.4.0 lib/rosetta_queue/adapter.rb