Class: DatabaseCleaner::Base

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Comparable
Defined in:
lib/database_cleaner/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(desired_orm = nil, opts = {}) ⇒ Base

Returns a new instance of Base.



15
16
17
18
19
20
21
# File 'lib/database_cleaner/base.rb', line 15

def initialize(desired_orm = nil, opts = {})
  @orm_autodetector = ORMAutodetector.new
  self.orm = desired_orm
  self.db = opts[:connection] || opts[:model] if opts.has_key?(:connection) || opts.has_key?(:model)
  self.strategy = orm_module && orm_module.default_strategy
  Safeguard.new.run
end

Instance Attribute Details

#ormObject

Returns the value of attribute orm.



48
49
50
# File 'lib/database_cleaner/base.rb', line 48

def orm
  @orm
end

Instance Method Details

#<=>(other) ⇒ Object



11
12
13
# File 'lib/database_cleaner/base.rb', line 11

def <=>(other)
  [orm, db] <=> [other.orm, other.db]
end

#auto_detected?Boolean

TODO remove the following methods in 2.0

Returns:

  • (Boolean)


67
68
69
70
# File 'lib/database_cleaner/base.rb', line 67

def auto_detected?
  DatabaseCleaner.deprecate "Calling `DatabaseCleaner[...].auto_detected?` is deprecated, and will be removed in database_cleaner 2.0 with no replacement."
  @orm_autodetector.autodetected?
end

#autodetect_ormObject



72
73
74
75
# File 'lib/database_cleaner/base.rb', line 72

def autodetect_orm
  DatabaseCleaner.deprecate "Calling `DatabaseCleaner[...].autodetect_orm` is deprecated, and will be removed in database_cleaner 2.0 with no replacement."
  @orm_autodetector.orm
end

#clean!Object



77
78
79
80
# File 'lib/database_cleaner/base.rb', line 77

def clean!
  DatabaseCleaner.deprecate "Calling `DatabaseCleaner[...].clean!` is deprecated, and will be removed in database_cleaner 2.0. Use `DatabaseCleaner[...].clean instead."
  clean
end

#clean_with(*args) ⇒ Object



58
59
60
61
62
63
# File 'lib/database_cleaner/base.rb', line 58

def clean_with(*args)
  strategy = create_strategy(*args)
  set_strategy_db strategy, db
  strategy.clean
  strategy
end

#clean_with!Object



82
83
84
85
# File 'lib/database_cleaner/base.rb', line 82

def clean_with!
  DatabaseCleaner.deprecate "Calling `DatabaseCleaner[...].clean_with!` is deprecated, and will be removed in database_cleaner 2.0. Use `DatabaseCleaner[...].clean_with instead."
  clean_with
end

#create_strategy(*args) ⇒ Object



107
108
109
110
111
112
113
# File 'lib/database_cleaner/base.rb', line 107

def create_strategy(*args)
  if DatabaseCleaner.called_externally?(__FILE__, caller)
    DatabaseCleaner.deprecate "Calling `DatabaseCleaner[...].create_strategy` is deprecated, and will be removed in database_cleaner 2.0. Use `DatabaseCleaner[...].strategy=` instead."
  end
  strategy, *strategy_args = args
  orm_strategy(strategy).new(*strategy_args)
end

#dbObject



27
28
29
# File 'lib/database_cleaner/base.rb', line 27

def db
  @db ||= :default
end

#db=(desired_db) ⇒ Object



23
24
25
# File 'lib/database_cleaner/base.rb', line 23

def db=(desired_db)
  @db = self.strategy_db = desired_db
end

#set_strategy_db(strategy, desired_db) ⇒ Object



96
97
98
99
100
101
102
103
104
105
# File 'lib/database_cleaner/base.rb', line 96

def set_strategy_db(strategy, desired_db)
  if DatabaseCleaner.called_externally?(__FILE__, caller)
    DatabaseCleaner.deprecate "Calling `DatabaseCleaner[...].set_strategy_db=` is deprecated, and will be removed in database_cleaner 2.0. Use `DatabaseCleaner[...].db=` instead."
  end
  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

#strategyObject



44
45
46
# File 'lib/database_cleaner/base.rb', line 44

def strategy
  @strategy ||= NullStrategy.new
end

#strategy=(args) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/database_cleaner/base.rb', line 31

def strategy=(args)
  strategy, *strategy_args = args
  @strategy = if strategy.is_a?(Symbol)
    create_strategy(*args)
  elsif strategy_args.empty?
    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, db
end

#strategy_db=(desired_db) ⇒ Object

TODO privatize the following methods in 2.0



89
90
91
92
93
94
# File 'lib/database_cleaner/base.rb', line 89

def strategy_db=(desired_db)
  if DatabaseCleaner.called_externally?(__FILE__, caller)
    DatabaseCleaner.deprecate "Calling `DatabaseCleaner[...].strategy_db=` is deprecated, and will be removed in database_cleaner 2.0. Use `DatabaseCleaner[...].db=` instead."
  end
  set_strategy_db(strategy, desired_db)
end