Class: DatabaseCleaner::Cleaner

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(orm, db: nil) ⇒ Cleaner

Returns a new instance of Cleaner.


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

def initialize(orm, db: nil)
  @orm = orm
  self.db = db
end

Instance Attribute Details

#ormObject (readonly)

Returns the value of attribute orm.


32
33
34
# File 'lib/database_cleaner/cleaner.rb', line 32

def orm
  @orm
end

Class Method Details

.available_strategies(orm_module) ⇒ Object


9
10
11
12
13
14
15
16
17
18
19
# File 'lib/database_cleaner/cleaner.rb', line 9

def self.available_strategies(orm_module)
  # introspect publically available constants for descendents of Strategy to get list of strategies
  # ignore classes named Base, because its a common name for a shared base class that adds ORM access stuff to Strategy before being inherited by final concrete class
  # if you're writing an adapter and this method is falsely returning an internal constant that isn't a valid strategy, consider making it private with Module#private_constant.
  orm_module.constants.select do |constant_name|
    ancestors = orm_module.const_get(constant_name).ancestors rescue []
    ancestors.include?(DatabaseCleaner::Strategy)
  end.map do |constant_name|
    underscore(constant_name).to_sym
  end - [:base]
end

Instance Method Details

#<=>(other) ⇒ Object


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

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

#clean_with(*args) ⇒ Object


62
63
64
65
66
67
# File 'lib/database_cleaner/cleaner.rb', line 62

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

#dbObject


38
39
40
# File 'lib/database_cleaner/cleaner.rb', line 38

def db
  @db ||= :default
end

#db=(desired_db) ⇒ Object


34
35
36
# File 'lib/database_cleaner/cleaner.rb', line 34

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

#strategyObject


55
56
57
# File 'lib/database_cleaner/cleaner.rb', line 55

def strategy
  @strategy ||= NullStrategy.new
end

#strategy=(args) ⇒ Object


42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/database_cleaner/cleaner.rb', line 42

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