Class: Etna::CsvImporter
- Inherits:
-
Object
- Object
- Etna::CsvImporter
- Defined in:
- lib/etna/csvs.rb
Direct Known Subclasses
Defined Under Namespace
Classes: ImportError, NestedRowProcessor
Constant Summary collapse
- COLUMN_AS_BOOLEAN =
-> (s) { ['true', 't', 'y', 'yes'].include?(s&.downcase) }
Instance Method Summary collapse
- #each_csv_row(filename: nil, input_io: nil, &block) ⇒ Object
-
#initialize(strip: true, filter_empties: true, &row_formatter) ⇒ CsvImporter
constructor
A new instance of CsvImporter.
- #replace_row_column(row, column, &block) ⇒ Object
Constructor Details
#initialize(strip: true, filter_empties: true, &row_formatter) ⇒ CsvImporter
Returns a new instance of CsvImporter.
3 4 5 6 7 8 9 10 11 12 |
# File 'lib/etna/csvs.rb', line 3 def initialize( strip: true, filter_empties: true, &row_formatter ) # Removes any columns from a row that are empty strings. Allows for some simpler 'empty' processing. This occurs # post stripping as well. @filter_empties = filter_empties @row_formatter = row_formatter end |
Instance Method Details
#each_csv_row(filename: nil, input_io: nil, &block) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/etna/csvs.rb', line 16 def each_csv_row(filename: nil, input_io: nil, &block) if input_io.nil? unless filename.nil? File.open(filename, 'r') do |io| return each_csv_row(input_io: io, &block) end end end lineno = 1 CSV.parse(input_io, headers: true, header_converters: :symbol) do |row| lineno += 1 row = row.to_hash row.keys.each { |k| row[k].strip! if row[k] =~ /^\s+$/ } if @strip row.keys.each { |k| row[k] = "" if row[k].nil? } unless @filter_empties row.select! { |k, v| !v.nil? && !v.empty? } if @filter_empties @row_formatter.call(row) unless @row_formatter.nil? yield row, lineno if block_given? end end |
#replace_row_column(row, column, &block) ⇒ Object
37 38 39 40 41 |
# File 'lib/etna/csvs.rb', line 37 def replace_row_column(row, column, &block) if !row[column].nil? || block.arity == 0 row[column] = yield row[column] end end |