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
22
23
24
25
26
27
# File 'lib/database_cleaner/base.rb', line 15

def initialize(desired_orm = nil, opts = {})
  @orm_autodetector = ORMAutodetector.new
  self.orm = desired_orm
  if opts.has_key?(:model)
    DatabaseCleaner.deprecate "Using the `:model` key in `DatabaseCleaner[:orm, model: ...]` is deprecated, and will be removed in database_cleaner 2.0. Please use the new `:db` key, instead, which has identical behavior: `DatabaseCleaner[:orm, db: ...]`."
  end
  if opts.has_key?(:connection)
    DatabaseCleaner.deprecate "Using the `:connection` key in `DatabaseCleaner[:orm, connection: ...]` is deprecated, and will be removed in database_cleaner 2.0. Please use the new `:db` key, instead, which has identical behavior: `DatabaseCleaner[:orm, db: ...]`."
  end
  self.db = opts[:db] || opts[:connection] || opts[:model] if opts.has_key?(:db) || 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.



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

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)


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

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



88
89
90
91
# File 'lib/database_cleaner/base.rb', line 88

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



93
94
95
96
# File 'lib/database_cleaner/base.rb', line 93

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



74
75
76
77
78
79
# File 'lib/database_cleaner/base.rb', line 74

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

#clean_with!Object



98
99
100
101
# File 'lib/database_cleaner/base.rb', line 98

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



123
124
125
126
127
128
129
# File 'lib/database_cleaner/base.rb', line 123

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



33
34
35
# File 'lib/database_cleaner/base.rb', line 33

def db
  @db ||= :default
end

#db=(desired_db) ⇒ Object



29
30
31
# File 'lib/database_cleaner/base.rb', line 29

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

#set_strategy_db(strategy, desired_db) ⇒ Object



112
113
114
115
116
117
118
119
120
121
# File 'lib/database_cleaner/base.rb', line 112

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



57
58
59
# File 'lib/database_cleaner/base.rb', line 57

def strategy
  @strategy ||= NullStrategy.new
end

#strategy=(args) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/database_cleaner/base.rb', line 37

def strategy=(args)
  strategy, *strategy_args = args

  if DatabaseCleaner.called_externally?(__FILE__, caller) \
    && [:redis, :mongo, :mongoid].include?(orm) \
    && strategy == :truncation
    DatabaseCleaner.deprecate "The #{orm} adapter's :truncation strategy will be renamed to :deletion in database_cleaner-#{orm} 2.0. Please specify the :deletion adapter to resolve this deprecation notice."
  end

  @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



105
106
107
108
109
110
# File 'lib/database_cleaner/base.rb', line 105

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