require 'csv' module Eco class CSV < ::CSV class << self include Eco::Data::Files # @return [Eco::CSV::Table] def parse(data, **kargs, &block) kargs = {headers: true, skip_blanks: true}.merge(kargs) Eco::CSV::Table.new(super(data, **kargs, &block)) end # @return [Eco::CSV::Table] def read(file, **kargs) params = {}.tap do |prms| prms.merge!(encoding: kargs.delete(:encoding)) if kargs.key?(:encoding) end parse(get_file_content(file, **params), **kargs) end # Splits the csv `filename` into `max_rows` # @yield [row, ridx, fidx, file] # @yieldparam row [Integer] the row # @yieldparam ridx [Integer] the index of the row in the source file # @yieldparam fidx [Integer] the number of the file # @yieldparam file [String] the default name of the file # @yieldreturn [Bollean] whether the row should be included # @param filename [String] the orignal file # @param max_rows [Integer] number of rows per file # @param start_at [Integer] row that sets the starting point. # Leave empty for the full set of rows. # @see Eco::CSV::Split#call def split(filename, max_rows:, start_at: nil, **kargs, &block) Eco::CSV::Split.new( filename, max_rows: max_rows, start_at: start_at, **kargs ).call(&block) end # @note it excludes headers by default # @return [Integer] the number of rows of the file def count(filename, start_at: nil, **kargs) count = 0 streamer = Eco::CSV::Stream.new(filename, **kargs) streamer.for_each(start_at_idx: start_at) do |row| included = true included = yield(row) if block_given? count += 1 if included end count end end end end require_relative 'csv/table' require_relative 'csv/stream' require_relative 'csv/split'