Sha256: a132ed6b621280bd9393d7eb0df5b318374c6421920f1edf99b53a0f656f1793

Contents?: true

Size: 1.6 KB

Versions: 5

Compression:

Stored size: 1.6 KB

Contents

require 'active_repository/adapters/active_hash_adapter'
require 'active_repository/adapters/default_adapter'
require 'active_repository/adapters/mongoid_adapter'

class PersistenceAdapter
  class << self
    def get_adapter(klass)
      modules = klass.get_model_class.included_modules.map(&:to_s)
      if modules.include?("Mongoid::Document")
        MongoidAdapter
      elsif modules.map(&:to_s).include?("DataMapper::Resource")
        DataMapperAdapter
      # elsif modules.map(&:to_s).include?("MongoMapper::Document")
      #   MongoMapperAdapter
      else
        DefaultAdapter
      end
    end

    def all(klass)
      get_adapter(klass).all(klass)
    end

    def create(klass, attributes)
      get_adapter(klass).create(klass, attributes)
    end

    def delete(klass, id)
      get_adapter(klass).delete(klass, id)
    end

    def delete_all(klass)
      get_adapter(klass).delete_all(klass)
    end

    def exists?(klass, id)
      get_adapter(klass).exists?(klass, id)
    end

    def find(klass, id)
      get_adapter(klass).find(klass, id)
    end

    def first(klass)
      get_adapter(klass).first(klass)
    end

    def last(klass)
      get_adapter(klass).last(klass)
    end

    def update_attribute(klass, id, key, value)
      get_adapter(klass).update_attribute(klass, id, key, value)
    end

    def update_attributes(klass, id, attributes)
      get_adapter(klass).update_attributes(klass, id, attributes)
    end

    def where(klass, args)
      get_adapter(klass).where(klass, args)
    end
  end

  def method_missing(sym, *args, &block)
    get_adapter(args.first).send(sym, args)
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
active_repository-0.2.7 lib/active_repository/adapters/persistence_adapter.rb
active_repository-0.2.6 lib/active_repository/adapters/persistence_adapter.rb
active_repository-0.2.5 lib/active_repository/adapters/persistence_adapter.rb
active_repository-0.2.4 lib/active_repository/adapters/persistence_adapter.rb
active_repository-0.2.3 lib/active_repository/adapters/persistence_adapter.rb