Sha256: afb9ab8b497f58ef3779b37a9c6f2bf89d1fc300ffd8b3d5967c47f4e64f2af1

Contents?: true

Size: 1.26 KB

Versions: 3

Compression:

Stored size: 1.26 KB

Contents

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

module Adapter
  extend Asserts
  include Defaults

  def self.definitions
    @definitions ||= {}
  end

  def self.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

  def self.adapters
    @adapters ||= {}
  end

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

  private
    def self.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]

        alias get read
        alias set write

        alias []  read
        alias []= write

        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

3 entries across 3 versions & 1 rubygems

Version Path
adapter-0.5.2 lib/adapter.rb
adapter-0.5.1 lib/adapter.rb
adapter-0.5 lib/adapter.rb