Expose ORM autodetection publicly

This commit is contained in:
Jeff Felchner 2013-05-27 16:53:36 -05:00
parent 7effdb71cb
commit 40099be196
2 changed files with 31 additions and 25 deletions

View file

@ -1,3 +1,8 @@
$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__))) unless $LOAD_PATH.include?(File.expand_path(File.dirname(__FILE__))) $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__))) unless $LOAD_PATH.include?(File.expand_path(File.dirname(__FILE__)))
require 'database_cleaner/configuration' require 'database_cleaner/configuration'
module DatabaseCleaner
def self.can_detect_orm?
DatabaseCleaner::Base.autodetect_orm
end
end

View file

@ -1,7 +1,6 @@
require 'database_cleaner/null_strategy' require 'database_cleaner/null_strategy'
module DatabaseCleaner module DatabaseCleaner
class Base class Base
def initialize(desired_orm = nil,opts = {}) def initialize(desired_orm = nil,opts = {})
if [:autodetect, nil, "autodetect"].include?(desired_orm) if [:autodetect, nil, "autodetect"].include?(desired_orm)
autodetect autodetect
@ -98,6 +97,28 @@ module DatabaseCleaner
self.orm == other.orm && self.db == other.db self.orm == other.orm && self.db == other.db
end end
def autodetect_orm
if defined? ::ActiveRecord
:active_record
elsif defined? ::DataMapper
:data_mapper
elsif defined? ::MongoMapper
:mongo_mapper
elsif defined? ::Mongoid
:mongoid
elsif defined? ::CouchPotato
:couch_potato
elsif defined? ::Sequel
:sequel
elsif defined? ::Moped
:moped
elsif defined? ::Ohm
:ohm
elsif defined? ::Redis
:redis
end
end
private private
def orm_module def orm_module
@ -116,30 +137,10 @@ module DatabaseCleaner
end end
def autodetect def autodetect
@orm ||= begin @autodetected = true
@autodetected = true
if defined? ::ActiveRecord @orm ||= autodetect_orm ||
:active_record raise(NoORMDetected, "No known ORM was detected! Is ActiveRecord, DataMapper, Sequel, MongoMapper, Mongoid, Moped, or CouchPotato, Redis or Ohm loaded?")
elsif defined? ::DataMapper
:data_mapper
elsif defined? ::MongoMapper
:mongo_mapper
elsif defined? ::Mongoid
:mongoid
elsif defined? ::CouchPotato
:couch_potato
elsif defined? ::Sequel
:sequel
elsif defined? ::Moped
:moped
elsif defined? ::Ohm
:ohm
elsif defined? ::Redis
:redis
else
raise NoORMDetected, "No known ORM was detected! Is ActiveRecord, DataMapper, Sequel, MongoMapper, Mongoid, Moped, or CouchPotato, Redis or Ohm loaded?"
end
end
end end
def set_default_orm_strategy def set_default_orm_strategy