Sha256: 8f801f113aa1ecb35a2cbf4c7487c28946ee3a1187c42af5c66042624f873b67
Contents?: true
Size: 1.03 KB
Versions: 14
Compression:
Stored size: 1.03 KB
Contents
# frozen_string_literal: true module Sidekiq module Job module Iterable # @api private class CsvEnumerator def initialize(csv) unless defined?(CSV) && csv.instance_of?(CSV) raise ArgumentError, "CsvEnumerator.new takes CSV object" end @csv = csv end def rows(cursor:) @csv.lazy .each_with_index .drop(cursor || 0) .to_enum { count_of_rows_in_file } end 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 private 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 end end end end
Version data entries
14 entries across 14 versions & 1 rubygems