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,26 +97,7 @@ module DatabaseCleaner
self.orm == other.orm && self.db == other.db self.orm == other.orm && self.db == other.db
end end
private def autodetect_orm
def orm_module
::DatabaseCleaner.orm_module(orm)
end
def orm_strategy(strategy)
require "database_cleaner/#{orm.to_s}/#{strategy.to_s}"
orm_module.const_get(strategy.to_s.capitalize)
rescue LoadError => e
if orm_module.respond_to? :available_strategies
raise UnknownStrategySpecified, "The '#{strategy}' strategy does not exist for the #{orm} ORM! Available strategies: #{orm_module.available_strategies.join(', ')}"
else
raise UnknownStrategySpecified, "The '#{strategy}' strategy does not exist for the #{orm} ORM!"
end
end
def autodetect
@orm ||= begin
@autodetected = true
if defined? ::ActiveRecord if defined? ::ActiveRecord
:active_record :active_record
elsif defined? ::DataMapper elsif defined? ::DataMapper
@ -136,10 +116,31 @@ module DatabaseCleaner
:ohm :ohm
elsif defined? ::Redis elsif defined? ::Redis
:redis :redis
end
end
private
def orm_module
::DatabaseCleaner.orm_module(orm)
end
def orm_strategy(strategy)
require "database_cleaner/#{orm.to_s}/#{strategy.to_s}"
orm_module.const_get(strategy.to_s.capitalize)
rescue LoadError => e
if orm_module.respond_to? :available_strategies
raise UnknownStrategySpecified, "The '#{strategy}' strategy does not exist for the #{orm} ORM! Available strategies: #{orm_module.available_strategies.join(', ')}"
else else
raise NoORMDetected, "No known ORM was detected! Is ActiveRecord, DataMapper, Sequel, MongoMapper, Mongoid, Moped, or CouchPotato, Redis or Ohm loaded?" raise UnknownStrategySpecified, "The '#{strategy}' strategy does not exist for the #{orm} ORM!"
end end
end end
def autodetect
@autodetected = true
@orm ||= autodetect_orm ||
raise(NoORMDetected, "No known ORM was detected! Is ActiveRecord, DataMapper, Sequel, MongoMapper, Mongoid, Moped, or CouchPotato, Redis or Ohm loaded?")
end end
def set_default_orm_strategy def set_default_orm_strategy