class Roo::CSV

Constants

TYPE_MAP

Attributes

filename[R]
filename_or_stream[R]

Public Instance Methods

cell(row, col, sheet = nil) click to toggle source
# File lib/roo/csv.rb, line 24
def cell(row, col, sheet = nil)
  sheet ||= default_sheet
  read_cells(sheet)
  @cell[normalize(row, col)]
end
cell_postprocessing(_row, _col, value) click to toggle source
# File lib/roo/csv.rb, line 36
def cell_postprocessing(_row, _col, value)
  value
end
celltype(row, col, sheet = nil) click to toggle source
# File lib/roo/csv.rb, line 30
def celltype(row, col, sheet = nil)
  sheet ||= default_sheet
  read_cells(sheet)
  @cell_type[normalize(row, col)]
end
csv_options() click to toggle source
# File lib/roo/csv.rb, line 40
def csv_options
  @options[:csv_options] || {}
end
set_type(row, col, type, _sheet) click to toggle source
# File lib/roo/csv.rb, line 48
def set_type(row, col, type, _sheet)
  @cell_type[[row, col]] = type
end
set_value(row, col, value, _sheet) click to toggle source
# File lib/roo/csv.rb, line 44
def set_value(row, col, value, _sheet)
  @cell[[row, col]] = value
end
sheets() click to toggle source

Returns an array with the names of the sheets. In CSV class there is only one dummy sheet, because a csv file cannot have more than one sheet.

# File lib/roo/csv.rb, line 20
def sheets
  ["default"]
end

Private Instance Methods

celltype_class(value) click to toggle source
# File lib/roo/csv.rb, line 61
def celltype_class(value)
  TYPE_MAP[value.class]
end
clean_sheet(sheet) click to toggle source
# File lib/roo/csv.rb, line 129
def clean_sheet(sheet)
  read_cells(sheet)

  @cell.each_pair do |coord, value|
    @cell[coord] = sanitize_value(value) if value.is_a?(::String)
  end

  @cleaned[sheet] = true
end
csv_foreach(path_or_io, options, &block) click to toggle source
# File lib/roo/csv.rb, line 105
def csv_foreach(path_or_io, options, &block)
  if is_stream?(path_or_io)
    ::CSV.new(path_or_io, **options).each(&block)
  else
    ::CSV.foreach(path_or_io, **options, &block)
  end
end
each_row(options, &block) click to toggle source
# File lib/roo/csv.rb, line 90
def each_row(options, &block)
  if uri?(filename)
    each_row_using_tempdir(options, &block)
  else
    csv_foreach(filename_or_stream, options, &block)
  end
end
each_row_using_tempdir(options, &block) click to toggle source
# File lib/roo/csv.rb, line 98
def each_row_using_tempdir(options, &block)
  ::Dir.mktmpdir(Roo::TEMP_PREFIX, ENV["ROO_TMP"]) do |tmpdir|
    tmp_filename = download_uri(filename, tmpdir)
    csv_foreach(tmp_filename, options, &block)
  end
end
read_cells(sheet = default_sheet) click to toggle source
# File lib/roo/csv.rb, line 65
def read_cells(sheet = default_sheet)
  sheet ||= default_sheet
  return if @cells_read[sheet]
  row_num = 0
  max_col_num = 0

  each_row csv_options do |row|
    row_num += 1
    col_num = 0

    row.each do |elem|
      col_num += 1
      coordinate = [row_num, col_num]
      @cell[coordinate] = elem
      @cell_type[coordinate] = celltype_class(elem)
    end

    max_col_num = col_num if col_num > max_col_num
  end

  set_row_count(sheet, row_num)
  set_column_count(sheet, max_col_num)
  @cells_read[sheet] = true
end
set_column_count(sheet, last_col) click to toggle source
# File lib/roo/csv.rb, line 121
def set_column_count(sheet, last_col)
  @first_column[sheet] = 1
  @last_column[sheet] = last_col
  @last_column[sheet] = @first_column[sheet] if @last_column[sheet].zero?

  nil
end
set_row_count(sheet, last_row) click to toggle source
# File lib/roo/csv.rb, line 113
def set_row_count(sheet, last_row)
  @first_row[sheet] = 1
  @last_row[sheet] = last_row
  @last_row[sheet] = @first_row[sheet] if @last_row[sheet].zero?

  nil
end