class Eco::API::UseCases::Default::Utils::SplitCsv < Eco::API::Common::Loaders::UseCase require_relative 'cli/split_csv_cli' MAX_ROWS = 15_000 name "split-csv" type :other def main(*_args) if simulate? count = Eco::CSV.count(input_file, start_at: start_at) log(:info) { "CSV '#{input_file}' has #{count} rows." } else Eco::CSV.split( input_file, max_rows: max_rows, start_at: start_at, &filter ).tap do |split| msg = [] msg << "Total rows copied: #{split.copy_count} (out of #{split.total_count})" msg << "Generated files:" split.out_files.each do |file| msg << " * #{file}'" end log(:info) { msg.join("\n") } end end end private def filter nil end def input_file options.dig(:source, :file) end def max_rows max_rows_options || self.class::MAX_ROWS end def max_rows_options return nil unless (num = options.dig(:output, :file, :max_rows)) num = num.to_i num = nil if num.zero? num end def start_at return nil unless (num = options.dig(:output, :file, :start_at)) num = num.to_i num = nil if num.zero? num end end