Class: Spreadshot::Backends::SmarterCSVBackend

Inherits:
ReaderBackend show all
Defined in:
lib/spreadshot/backends/smarter_csv_backend.rb

Overview

Adapter for the SmarterCSV Gem (https://github.com/tilo/smarter_csv)

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ SmarterCSVBackend

Note:

Ignores :header option

Returns a new instance of SmarterCSVBackend (See Spreadshot::ReaderBackends::ReaderBackend)


12
13
14
15
16
# File 'lib/spreadshot/backends/smarter_csv_backend.rb', line 12

def initialize(options = {})
  options ||= {}
  options[:headers] = true
  super(options)
end

Instance Method Details

#read(path_to_spreadsheet) {|row_index, row_data| ... } ⇒ Object

Reads data from the specified CSV file (See Spreadshot::ReaderBackends::ReaderBackend)

Yields:

  • (row_index, row_data)

Yield Parameters:

  • row_index (Integer)

    The index of the current row being read. The first row has an index of 1

  • row_data (Hash)

    A hash representation of the data read from the current row

Raises:


33
34
35
36
37
38
39
40
41
# File 'lib/spreadshot/backends/smarter_csv_backend.rb', line 33

def read(path_to_spreadsheet)
  SmarterCSV.process(path_to_spreadsheet) do |row|
    current_row_data = row.first
    yield(@current_row_index, current_row_data)
    @current_row_index += 1
  end
rescue => e
  raise Spreadshot::ReaderError, e.message
end