Class: DatabaseRewinder::Cleaner

Inherits:
Object
  • Object
show all
Defined in:
lib/database_rewinder/cleaner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(db: nil, connection_name: nil, only: nil, except: nil) ⇒ Cleaner

Returns a new instance of Cleaner.



5
6
7
8
# File 'lib/database_rewinder/cleaner.rb', line 5

def initialize(db: nil, connection_name: nil, only: nil, except: nil)
  @db, @connection_name, @only, @except = db, connection_name, Array(only), Array(except)
  reset
end

Instance Attribute Details

#connection_nameObject

Returns the value of attribute connection_name.



3
4
5
# File 'lib/database_rewinder/cleaner.rb', line 3

def connection_name
  @connection_name
end

#dbObject

Returns the value of attribute db.



3
4
5
# File 'lib/database_rewinder/cleaner.rb', line 3

def db
  @db
end

#inserted_tablesObject

Returns the value of attribute inserted_tables.



3
4
5
# File 'lib/database_rewinder/cleaner.rb', line 3

def inserted_tables
  @inserted_tables
end

#poolObject

Returns the value of attribute pool.



3
4
5
# File 'lib/database_rewinder/cleaner.rb', line 3

def pool
  @pool
end

Instance Method Details

#cleanObject



10
11
12
13
14
15
16
# File 'lib/database_rewinder/cleaner.rb', line 10

def clean
  return unless pool
  return if inserted_tables.empty?

  delete_all (ar_conn = pool.connection), DatabaseRewinder.all_table_names(ar_conn) & inserted_tables
  reset
end

#clean_allObject



18
19
20
21
22
23
# File 'lib/database_rewinder/cleaner.rb', line 18

def clean_all
  return unless pool

  delete_all (ar_conn = pool.connection), DatabaseRewinder.all_table_names(ar_conn)
  reset
end

#clean_with(_strategy, only: nil, except: nil) ⇒ Object



25
26
27
28
29
# File 'lib/database_rewinder/cleaner.rb', line 25

def clean_with(_strategy, only: nil, except: nil, **)
  @only += Array(only) unless only.blank?
  @except += Array(except) unless except.blank?
  clean_all
end

#strategy=(_strategy, only: nil, except: nil) ⇒ Object

for database_cleaner compat



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

def strategy=(_strategy, only: nil, except: nil, **)
  @only += Array(only) unless only.blank?
  @except += Array(except) unless except.blank?
end