Sha256: 6ddcd02ecaa62ab1752595ad6ef1c0eb2bf2b1ab3cfb7c9becfff572e1c69273

Contents?: true

Size: 1.25 KB

Versions: 2

Compression:

Stored size: 1.25 KB

Contents

require 'adapter/asserts'
require 'adapter/defaults'
require 'adapter/exceptions'

module Adapter
  extend self
  extend Asserts

  # Stores the definitions for each adapter by name
  def definitions
    @definitions ||= {}
  end

  def define(name, mod=nil, &block)
    definition_module = Module.new
    definition_module.send(:include, Defaults)
    definition_module.send(:include, mod) unless mod.nil?
    definition_module.send(:include, Module.new(&block)) if block_given?
    assert_valid_module(definition_module)
    adapters.delete(name.to_sym)
    definitions[name.to_sym] = definition_module
  end

  # Memoizes adapter instances based on their definitions
  def adapters
    @adapters ||= {}
  end

  def [](name)
    assert_valid_adapter(name)
    adapters[name.to_sym] ||= get_adapter_instance(name)
  end

  private
    def get_adapter_instance(name)
      Class.new do
        attr_reader :client, :options

        def initialize(client, options={})
          @client = client
          @options = options
        end

        include Adapter.definitions[name.to_sym]

        def eql?(other)
          self.class.eql?(other.class) && client == other.client
        end
        alias == eql?

        class_eval "def name; :#{name} end"
      end
    end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
adapter-0.7.1 lib/adapter.rb
adapter-0.7.0 lib/adapter.rb