Class: CsvCutter::Csv
- Inherits:
-
Object
- Object
- CsvCutter::Csv
- Defined in:
- lib/csv_cutter/csv.rb
Instance Attribute Summary collapse
-
#file_mode ⇒ Object
readonly
Returns the value of attribute file_mode.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#out_dir ⇒ Object
readonly
Returns the value of attribute out_dir.
Instance Method Summary collapse
-
#initialize(headers: false, out_dir:, encoding: nil, col_sep: ',', quote_char: '"') ⇒ Csv
constructor
A new instance of Csv.
- #split_by_number_rows(file_path:, number_rows: 10000) ⇒ Object
Constructor Details
#initialize(headers: false, out_dir:, encoding: nil, col_sep: ',', quote_char: '"') ⇒ Csv
Returns a new instance of Csv.
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/csv_cutter/csv.rb', line 9 def initialize(headers: false, out_dir:, encoding: nil, col_sep: ',', quote_char: '"') = { headers: headers, encoding: encoding, col_sep: col_sep, quote_char: quote_char, } @out_dir = out_dir @file_mode = encoding ? "w:#{encoding}" : 'w' end |
Instance Attribute Details
#file_mode ⇒ Object (readonly)
Returns the value of attribute file_mode.
7 8 9 |
# File 'lib/csv_cutter/csv.rb', line 7 def file_mode @file_mode end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
7 8 9 |
# File 'lib/csv_cutter/csv.rb', line 7 def end |
#out_dir ⇒ Object (readonly)
Returns the value of attribute out_dir.
7 8 9 |
# File 'lib/csv_cutter/csv.rb', line 7 def out_dir @out_dir end |
Instance Method Details
#split_by_number_rows(file_path:, number_rows: 10000) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/csv_cutter/csv.rb', line 20 def split_by_number_rows(file_path:, number_rows: 10000) threads = [] file_name = File.basename(file_path, ".*") CSV.foreach(file_path, **).each_slice(number_rows).with_index(1) do |rows, idx| threads << Thread.new(idx) do |idx| CSV.open("#{out_dir}/#{file_name}_#{idx}.csv", file_mode, **.slice(:col_sep, :quote_char)) do |csv_file| rows.each_with_index do |row, idx| case row when CSV::Row csv_file << row.headers if idx == 0 csv_file << row.fields else csv_file << row end end end end end threads.each(&:join) true end |