class ActiverecordHoarder::RecordCollector

Attributes

relative_limit[R]

Public Class Methods

new(model_class) click to toggle source
# File lib/activerecord_hoarder/record_collector.rb, line 4
def initialize(model_class)
  @model_class = model_class
end

Public Instance Methods

in_batches(delete_on_success: false) { |batch| ... } click to toggle source
# File lib/activerecord_hoarder/record_collector.rb, line 8
def in_batches(delete_on_success: false)
  while collect_batch
    yield @batch
    next if !delete_on_success
    destroy_current_records!
  end
end

Private Instance Methods

activate_limit() click to toggle source
# File lib/activerecord_hoarder/record_collector.rb, line 30
def activate_limit
  @relative_limit = [@batch.date.end_of_week + 1, archive_timeframe_upper_limit].min
end
archive_timeframe_upper_limit() click to toggle source
# File lib/activerecord_hoarder/record_collector.rb, line 34
def archive_timeframe_upper_limit
  Time.now.getutc.beginning_of_day
end
batch_data_cached?() click to toggle source
# File lib/activerecord_hoarder/record_collector.rb, line 38
def batch_data_cached?
  @batch.present?
end
collect_batch() click to toggle source
# File lib/activerecord_hoarder/record_collector.rb, line 18
def collect_batch
  activate_limit if batch_data_cached? && !limit_toggled?
  if limit_toggled?
    @batch = ensuring_new_records do
      retrieve_next_batch
    end
  else
    @batch = retrieve_first_batch
  end
  batch_data_cached?
end
destroy_current_records!() click to toggle source
# File lib/activerecord_hoarder/record_collector.rb, line 42
def destroy_current_records!
  @model_class.connection.execute(@batch_query.delete(@batch.date))
end
ensuring_new_records() { || ... } click to toggle source
# File lib/activerecord_hoarder/record_collector.rb, line 46
def ensuring_new_records
  record_batch = yield
  @batch.date == record_batch.try(:date) ? nil : record_batch
end
limit_toggled?() click to toggle source
# File lib/activerecord_hoarder/record_collector.rb, line 51
def limit_toggled?
  @relative_limit.present?
end
retrieve_first_batch() click to toggle source
# File lib/activerecord_hoarder/record_collector.rb, line 55
def retrieve_first_batch
  @batch_query = ::ActiverecordHoarder::BatchQuery.new(archive_timeframe_upper_limit, @model_class)
  batch_data = @model_class.connection.exec_query(@batch_query.fetch)
  ::ActiverecordHoarder::Batch.from_records(batch_data)
end
retrieve_next_batch() click to toggle source
# File lib/activerecord_hoarder/record_collector.rb, line 61
def retrieve_next_batch
  @batch_query = ::ActiverecordHoarder::BatchQuery.new(relative_limit, @model_class)
  batch_data = @model_class.connection.exec_query(@batch_query.fetch)
  ::ActiverecordHoarder::Batch.from_records(batch_data)
end