module Ruport::Data::Table::FromCSV

Overview

This module provides facilities for creating tables from csv data.

Public Instance Methods

load(csv_file, options={},&block) click to toggle source

Loads a CSV file directly into a Table using the FasterCSV library.

Example:

# treat first row as column_names
table = Table.load('mydata.csv')

# do not assume the data has column_names
table = Table.load('mydata.csv',:has_names => false)

# pass in FasterCSV options, such as column separators
table = Table.load('mydata.csv',:csv_options => { :col_sep => "\t" })
# File lib/ruport/data/table.rb, line 169
def load(csv_file, options={},&block)
  get_table_from_csv(:foreach, csv_file, options,&block)
end
parse(string, options={},&block) click to toggle source

Creates a Table from a CSV string using FasterCSV. See Table.load for additional examples.

table = Table.parse("a,b,c\n1,2,3\n4,5,6\n")
# File lib/ruport/data/table.rb, line 178
def parse(string, options={},&block)
  get_table_from_csv(:parse,string,options,&block)
end

Private Instance Methods

adjust_for_headers(loaded,row,options) click to toggle source
# File lib/ruport/data/table.rb, line 225
def adjust_for_headers(loaded,row,options)
  if options[:has_names]
    loaded.column_names = row
  elsif options[:csv_options][:headers]
    loaded.column_names = row.headers
  end
end
adjust_options_for_fcsv_headers(options) click to toggle source
# File lib/ruport/data/table.rb, line 221
def adjust_options_for_fcsv_headers(options)
  options[:has_names] = false if options[:csv_options][:headers]
end
handle_csv_row_proc(feeder,row,options,block) click to toggle source
# File lib/ruport/data/table.rb, line 212
def handle_csv_row_proc(feeder,row,options,block)
  if options[:records]
    rc = options[:record_class] || Record
    row = rc.new(row, :attributes => feeder.data.column_names)
  end
  
  block[feeder,row]
end