Class: MaintenanceTasks::BatchCsvCollectionBuilder Private

Inherits:
CsvCollectionBuilder show all
Defined in:
app/models/maintenance_tasks/batch_csv_collection_builder.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Strategy for building a Task that processes CSV files in batches.

Defined Under Namespace

Classes: BatchCsv

Instance Method Summary collapse

Methods inherited from CsvCollectionBuilder

#has_csv_content?, #no_collection?

Constructor Details

#initialize(batch_size, **csv_options) ⇒ BatchCsvCollectionBuilder

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initialize a BatchCsvCollectionBuilder with a batch size.

Parameters:

  • batch_size (Integer)

    the number of CSV rows in a batch.

  • csv_options (Hash)

    options to pass to the CSV parser.



16
17
18
19
# File 'app/models/maintenance_tasks/batch_csv_collection_builder.rb', line 16

def initialize(batch_size, **csv_options)
  @batch_size = batch_size
  super(**csv_options)
end

Instance Method Details

#collection(task) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Defines the collection to be iterated over, based on the provided CSV. Includes the CSV and the batch size.



23
24
25
26
27
28
# File 'app/models/maintenance_tasks/batch_csv_collection_builder.rb', line 23

def collection(task)
  BatchCsv.new(
    csv: CSV.new(task.csv_content, **@csv_options),
    batch_size: @batch_size,
  )
end

#count(task) ⇒ Integer

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The number of batches to be processed. Excludes the header row from the count and assumes a trailing newline is at the end of the CSV file. Note that this number is an approximation based on the number of newlines.

Returns:

  • (Integer)

    the approximate number of batches to process.



36
37
38
39
# File 'app/models/maintenance_tasks/batch_csv_collection_builder.rb', line 36

def count(task)
  count = task.csv_content.count("\n") - 1
  (count + @batch_size - 1) / @batch_size
end