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) ⇒ 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.



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

def initialize(batch_size)
  @batch_size = batch_size
  super()
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.



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

def collection(task)
  BatchCsv.new(
    csv: CSV.new(task.csv_content, headers: true),
    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.



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

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