lib/exporter/exporters/csv_exporter.rb in exporter-0.0.3 vs lib/exporter/exporters/csv_exporter.rb in exporter-0.1.0
- old
+ new
@@ -1,25 +1,21 @@
-require 'CSV'
module Exporter
class CsvExporter < Exporter
- def self.process(data, options)
- raise TypeError.new('data must be an array') unless data.class.eql? Array
- raise TypeError.new('Data must be an ActiveRecord') unless data[0].kind_of? ActiveRecord::Base
+ def process(data, options)
- if options[:columns]
- columns = options[:columns]
- else
- columns = data[0].class.attribute_names
- end
+ raise TypeError.new unless is_active_record?(data, options)
+ columns = options[:columns] || data[0].class.attribute_names
+
data = CSV.generate do |csv|
csv << columns
data.each do |d|
csv << d.attributes.values_at(*columns)
end
end
CsvDocument.new(data)
end
+
end
-end
\ No newline at end of file
+end