class Sidekiq::Job::Iterable::CsvEnumerator

@api private

Public Class Methods

new(csv) click to toggle source
# File lib/sidekiq/job/iterable/csv_enumerator.rb, line 8
def initialize(csv)
  unless defined?(CSV) && csv.instance_of?(CSV)
    raise ArgumentError, "CsvEnumerator.new takes CSV object"
  end

  @csv = csv
end

Public Instance Methods

batches(cursor:, batch_size: 100) click to toggle source
# File lib/sidekiq/job/iterable/csv_enumerator.rb, line 23
def batches(cursor:, batch_size: 100)
  @csv.lazy
    .each_slice(batch_size)
    .with_index
    .drop(cursor || 0)
    .to_enum { (count_of_rows_in_file.to_f / batch_size).ceil }
end
rows(cursor:) click to toggle source
# File lib/sidekiq/job/iterable/csv_enumerator.rb, line 16
def rows(cursor:)
  @csv.lazy
    .each_with_index
    .drop(cursor || 0)
    .to_enum { count_of_rows_in_file }
end

Private Instance Methods

count_of_rows_in_file() click to toggle source
# File lib/sidekiq/job/iterable/csv_enumerator.rb, line 33
def count_of_rows_in_file
  filepath = @csv.path
  return unless filepath

  count = IO.popen(["wc", "-l", filepath]) do |out|
    out.read.strip.to_i
  end

  count -= 1 if @csv.headers
  count
end