Sha256: c82d186a204efb3624cec903484c8d32f6413faaba18e51595e650ad1c2f44b3
Contents?: true
Size: 1.77 KB
Versions: 29
Compression:
Stored size: 1.77 KB
Contents
module Eco class CSV class Stream include Eco::Language::AuxiliarLogger attr_reader :filename def initialize(filename, **kargs) raise ArgumentError, "File '#{filename}' does not exist" unless ::File.exist?(filename) @filename = filename @params = { headers: true, skip_blanks: true }.merge(kargs) init end def for_each(start_at_idx: 0) raise ArgumentError, 'Expecting block, but not given.' unless block_given? move_to_idx(start_at_idx) yield(row, next_idx) while (self.row = csv.shift) rescue StandardError => err self.exception = err raise ensure (fd.close; @fd = nil) if fd.is_a?(::File) # rubocop:disable Style/Semicolon if exception # Give some feedback if it crashes msg = [] msg << "Last row IDX: #{idx}" msg << "Last row content: #{row.to_h.pretty_inspect}" puts msg log(:debug) { msg.join("\n") } end end def move_to_idx(start_at_idx) start_at_idx ||= 0 next_idx while (idx < start_at_idx) && (self.row = csv.shift) end private attr_reader :params attr_reader :idx, :fd attr_accessor :row, :exception def next_idx idx.tap { @idx += 1 } end # see https://dalibornasevic.com/posts/68-processing-large-csv-files-with-ruby def csv return @csv if instance_variable_defined?(:@csv) @fd = ::File.open(filename, 'r') @csv = Eco::CSV.new(fd, **params) end def init @idx ||= 0 # rubocop:disable Naming/MemoizedInstanceVariableName end end end end
Version data entries
29 entries across 29 versions & 1 rubygems