Module: Utilities::Csv

Defined in:
lib/utilities/csv.rb

Class Method Summary (collapse)

Class Method Details

+ (Object) to_csv(objects, options = {col_sep: "\t", headers: true, encoding: 'UTF-8'})

A dirt simple CSV dump to STDOUT, tab separators. Takes an array of AR instances. TODO:

- validate object collection is uniformly classed (all the same)


7
8
9
10
11
12
13
14
15
16
17
# File 'lib/utilities/csv.rb', line 7

def self.to_csv(objects, options = {col_sep: "\t", headers: true, encoding: 'UTF-8'})
  return if objects.size == 0
  column_names = objects.first.class.column_names
  string       = CSV.generate(options) do |csv|
    csv << column_names
    objects.each do |o|
      csv << o.attributes.values_at(*column_names)
    end
  end
  puts string
end