Class: DatabaseFlusher::ActiveRecord::DeletionStrategy

Inherits:
Object
  • Object
show all
Defined in:
lib/database_flusher/active_record/deletion_strategy.rb

Defined Under Namespace

Classes: Subscriber

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDeletionStrategy

Returns a new instance of DeletionStrategy.



28
29
30
# File 'lib/database_flusher/active_record/deletion_strategy.rb', line 28

def initialize
  @tables = Set.new
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



10
11
12
# File 'lib/database_flusher/active_record/deletion_strategy.rb', line 10

def adapter
  @adapter
end

#tablesObject (readonly)

Returns the value of attribute tables.



10
11
12
# File 'lib/database_flusher/active_record/deletion_strategy.rb', line 10

def tables
  @tables
end

Instance Method Details

#cleanObject



50
51
52
53
54
55
56
57
# File 'lib/database_flusher/active_record/deletion_strategy.rb', line 50

def clean
  return if tables.empty?

  # puts "Cleaning #{tables.inspect}"
  adapter.delete(*tables)

  tables.clear
end

#startObject



32
33
34
35
36
37
38
39
40
41
# File 'lib/database_flusher/active_record/deletion_strategy.rb', line 32

def start
  @subscriber ||= ActiveSupport::Notifications.subscribe(
    'sql.active_record',
    Subscriber.new(self)
  )
  connection = ::ActiveRecord::Base.connection
  @adapter = DatabaseFlusher::ActiveRecord.
    const_get("#{connection.adapter_name}Adapter").
    new(connection)
end

#stopObject



43
44
45
46
47
48
# File 'lib/database_flusher/active_record/deletion_strategy.rb', line 43

def stop
  if @subscriber
    ActiveSupport::Notifications.unsubscribe(@subscriber)
    @subscriber = nil
  end
end