Module: Workbook::Readers::CsvReader

Included in:
Book
Defined in:
lib/workbook/readers/csv_reader.rb

Instance Method Summary collapse

Instance Method Details

#csv_libObject



11
12
13
14
15
16
17
18
# File 'lib/workbook/readers/csv_reader.rb', line 11

def csv_lib
  if RUBY_VERSION < '1.9'
    require 'faster_csv'
    return FasterCSV
  else
    return CSV
  end
end

#load_csv(text) ⇒ Object



6
7
8
9
# File 'lib/workbook/readers/csv_reader.rb', line 6

def load_csv text
  csv = text
  parse_csv csv
end

#parse_csv(csv_raw) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/workbook/readers/csv_reader.rb', line 20

def parse_csv csv_raw
  custom_date_converter = Workbook::Cell.new.string_optimistic_date_converter
  converters = [:float,:integer,:date,:date_time,custom_date_converter]
  csv=nil
  #begin
  csv = csv_lib.parse(csv_raw,{:converters=>converters})

  #rescue
  # we're going to have another shot at it...
  #end

  if csv==nil or csv[0].count == 1
    csv_excel = csv_lib.parse(csv_raw,{:converters=>converters,:col_sep=>';'})
    csv = csv_excel if csv_excel[0].count > 1
  end

  self[0]=Workbook::Sheet.new(csv,self) unless sheet.has_contents?
end