Class: DatabaseCleaner::Base
- Inherits:
-
Object
- Object
- DatabaseCleaner::Base
- Defined in:
- lib/database_cleaner/base.rb
Instance Method Summary collapse
-
#==(other) ⇒ Object
TODO make strategies directly comparable.
- #auto_detected? ⇒ Boolean
- #autodetect_orm ⇒ Object
- #clean ⇒ Object (also: #clean!)
- #clean_with(*args) ⇒ Object (also: #clean_with!)
- #cleaning(&block) ⇒ Object
- #create_strategy(*args) ⇒ Object
- #db ⇒ Object
- #db=(desired_db) ⇒ Object
-
#initialize(desired_orm = nil, opts = {}) ⇒ Base
constructor
A new instance of Base.
- #orm ⇒ Object
- #orm=(desired_orm) ⇒ Object
- #set_strategy_db(strategy, desired_db) ⇒ Object
- #start ⇒ Object
- #strategy ⇒ Object
- #strategy=(args) ⇒ Object
- #strategy_db=(desired_db) ⇒ Object
Constructor Details
#initialize(desired_orm = nil, opts = {}) ⇒ Base
Returns a new instance of Base.
4 5 6 7 8 9 10 11 12 |
# File 'lib/database_cleaner/base.rb', line 4 def initialize(desired_orm = nil,opts = {}) if [:autodetect, nil, "autodetect"].include?(desired_orm) autodetect else self.orm = desired_orm end self.db = opts[:connection] || opts[:model] if opts.has_key?(:connection) || opts.has_key?(:model) set_default_orm_strategy end |
Instance Method Details
#==(other) ⇒ Object
TODO make strategies directly comparable
100 101 102 |
# File 'lib/database_cleaner/base.rb', line 100 def ==(other) self.orm == other.orm && self.db == other.db end |
#auto_detected? ⇒ Boolean
95 96 97 |
# File 'lib/database_cleaner/base.rb', line 95 def auto_detected? !!@autodetected end |
#autodetect_orm ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/database_cleaner/base.rb', line 104 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 |
#clean ⇒ Object Also known as: clean!
85 86 87 |
# File 'lib/database_cleaner/base.rb', line 85 def clean strategy.clean end |
#clean_with(*args) ⇒ Object Also known as: clean_with!
36 37 38 39 40 41 42 |
# File 'lib/database_cleaner/base.rb', line 36 def clean_with(*args) strategy = create_strategy(*args) set_strategy_db strategy, self.db strategy.clean strategy end |
#cleaning(&block) ⇒ Object
91 92 93 |
# File 'lib/database_cleaner/base.rb', line 91 def cleaning(&block) strategy.cleaning(&block) end |
#create_strategy(*args) ⇒ Object
31 32 33 34 |
# File 'lib/database_cleaner/base.rb', line 31 def create_strategy(*args) strategy, *strategy_args = args orm_strategy(strategy).new(*strategy_args) end |
#db ⇒ Object
27 28 29 |
# File 'lib/database_cleaner/base.rb', line 27 def db @db ||= :default end |
#db=(desired_db) ⇒ Object
14 15 16 17 |
# File 'lib/database_cleaner/base.rb', line 14 def db=(desired_db) self.strategy_db = desired_db @db = desired_db end |
#orm ⇒ Object
77 78 79 |
# File 'lib/database_cleaner/base.rb', line 77 def orm @orm || autodetect end |
#orm=(desired_orm) ⇒ Object
73 74 75 |
# File 'lib/database_cleaner/base.rb', line 73 def orm=(desired_orm) @orm = desired_orm.to_sym end |
#set_strategy_db(strategy, desired_db) ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/database_cleaner/base.rb', line 46 def set_strategy_db(strategy, desired_db) if strategy.respond_to? :db= strategy.db = desired_db elsif desired_db != :default raise ArgumentError, "You must provide a strategy object that supports non default databases when you specify a database" end end |
#start ⇒ Object
81 82 83 |
# File 'lib/database_cleaner/base.rb', line 81 def start strategy.start end |
#strategy ⇒ Object
69 70 71 |
# File 'lib/database_cleaner/base.rb', line 69 def strategy @strategy ||= NullStrategy end |
#strategy=(args) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/database_cleaner/base.rb', line 54 def strategy=(args) strategy, *strategy_args = args if strategy.is_a?(Symbol) @strategy = create_strategy(*args) elsif strategy_args.empty? @strategy = strategy else raise ArgumentError, "You must provide a strategy object, or a symbol for a known strategy along with initialization params." end set_strategy_db @strategy, self.db @strategy end |
#strategy_db=(desired_db) ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/database_cleaner/base.rb', line 19 def strategy_db=(desired_db) if strategy.respond_to? :db= strategy.db = desired_db elsif desired_db!= :default raise ArgumentError, "You must provide a strategy object that supports non default databases when you specify a database" end end |