Class: ActiveRecordCSVImporter::CSVReader

Inherits:
Object
  • Object
show all
Defined in:
lib/activerecord_csv_importer/csv_reader.rb

Overview

Reads, sanitize and parse a CSV file

Instance Method Summary collapse

Instance Method Details

#csv_rowsObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/activerecord_csv_importer/csv_reader.rb', line 12

def csv_rows
  @csv_rows ||= begin
    sane_content = sanitize_content(read_content)
    separator = detect_separator(sane_content)
    cells = CSV.parse(
      sane_content,
      col_sep: separator,
      quote_char: quote_char,
      skip_blanks: true,
      encoding: encoding
    )
    sanitize_cells(cells)
  end
end

#headerObject

Returns the header as an Array of Strings



28
29
30
# File 'lib/activerecord_csv_importer/csv_reader.rb', line 28

def header
  @header ||= csv_rows.first
end

#rowsObject

Returns the rows as an Array of Arrays of Strings



33
34
35
# File 'lib/activerecord_csv_importer/csv_reader.rb', line 33

def rows
  @rows ||= csv_rows[1..-1]
end