Class: JobIteration::CsvEnumerator
- Inherits:
-
Object
- Object
- JobIteration::CsvEnumerator
- Defined in:
- lib/job-iteration/csv_enumerator.rb
Overview
CsvEnumerator makes it possible to write an Iteration job that uses CSV file as a collection to Iterate.
Instance Method Summary collapse
-
#batches(batch_size:, cursor:) ⇒ Enumerator
Constructs a enumerator on batches of CSV rows.
-
#initialize(csv) ⇒ JobIteration::CsvEnumerator
constructor
Constructs CsvEnumerator instance based on a CSV file.
-
#rows(cursor:) ⇒ Enumerator
Constructs a enumerator on CSV rows.
Constructor Details
#initialize(csv) ⇒ JobIteration::CsvEnumerator
Constructs CsvEnumerator instance based on a CSV file.
22 23 24 25 26 27 28 |
# File 'lib/job-iteration/csv_enumerator.rb', line 22 def initialize(csv) unless defined?(CSV) && csv.instance_of?(CSV) raise ArgumentError, "CsvEnumerator.new takes CSV object" end @csv = csv end |
Instance Method Details
#batches(batch_size:, cursor:) ⇒ Enumerator
Constructs a enumerator on batches of CSV rows
41 42 43 44 45 46 47 |
# File 'lib/job-iteration/csv_enumerator.rb', line 41 def batches(batch_size:, cursor:) @csv.lazy .each_slice(batch_size) .with_index .drop(count_of_processed_rows(cursor)) .to_enum { (count_of_rows_in_file.to_f / batch_size).ceil } end |
#rows(cursor:) ⇒ Enumerator
Constructs a enumerator on CSV rows
32 33 34 35 36 37 |
# File 'lib/job-iteration/csv_enumerator.rb', line 32 def rows(cursor:) @csv.lazy .each_with_index .drop(count_of_processed_rows(cursor)) .to_enum { count_of_rows_in_file } end |