Class: DatabaseCleaner::Cleaners

Inherits:
Hash
  • Object
show all
Defined in:
lib/database_cleaner/cleaners.rb

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ Cleaners

Returns a new instance of Cleaners.



6
7
8
# File 'lib/database_cleaner/cleaners.rb', line 6

def initialize hash={}
  super.replace(hash)
end

Instance Method Details

#[](orm, **opts) ⇒ Object

FIXME this method conflates creation with lookup… both a command and a query. yuck.

Raises:

  • (ArgumentError)


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

def [](orm, **opts)
  raise ArgumentError if orm.nil?
  fetch([orm, opts]) { add_cleaner(orm, **opts) }
end

#cleanObject



45
46
47
48
# File 'lib/database_cleaner/cleaners.rb', line 45

def clean
  Safeguard.new.run
  values.each { |cleaner| cleaner.clean }
end

#clean_with(*args) ⇒ Object



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

def clean_with(*args)
  Safeguard.new.run
  values.each { |cleaner| cleaner.clean_with(*args) }
end

#cleaning(&inner_block) ⇒ Object



50
51
52
53
54
55
# File 'lib/database_cleaner/cleaners.rb', line 50

def cleaning(&inner_block)
  Safeguard.new.run
  values.inject(inner_block) do |curr_block, cleaner|
    proc { cleaner.cleaning(&curr_block) }
  end.call
end

#startObject



40
41
42
43
# File 'lib/database_cleaner/cleaners.rb', line 40

def start
  Safeguard.new.run
  values.each { |cleaner| cleaner.start }
end

#strategyHash

It returns a hash with all the strategies associated with all the cleaners.

For example:

“‘ cleaners.strategy

>
:active_record_1 => :truncation,
:active_record_2 => :truncation,
:data_mapper_1 => :truncation

“‘

Returns:

  • (Hash)


31
32
33
# File 'lib/database_cleaner/cleaners.rb', line 31

def strategy
  transform_values(&:strategy)
end

#strategy=(strategy) ⇒ Object



35
36
37
38
# File 'lib/database_cleaner/cleaners.rb', line 35

def strategy=(strategy)
  values.each { |cleaner| cleaner.strategy = strategy }
  remove_duplicates
end