Module: Utilities::CSV

Defined in:
lib/utilities/csv.rb

Class Method Summary collapse

Class Method Details

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

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)

Parameters:

  • of (Array)

    objects

  • of (Hash)

    options



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

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