lib/exporters/csv_exporter.rb in datashift-0.13.0 vs lib/exporters/csv_exporter.rb in datashift-0.14.0
- old
+ new
@@ -10,19 +10,20 @@
require 'csv'
module DataShift
class CsvExporter < ExporterBase
-
+
+ include DataShift::Logging
+
attr_accessor :text_delim
def initialize(filename)
super(filename)
@text_delim = "\'"
end
-
# Return opposite of text delim - "hello, 'barry'" => '"hello, "barry""'
def escape_text_delim
return '"' if @text_delim == "\'"
"\'"
end
@@ -31,18 +32,26 @@
# Options :
# => :filename
# => :text_delim => Char to use to delim columns, useful when data contain embedded ','
# => ::methods => List of methods to additionally call on each record
#
- def export(records, options = {})
+ def export(export_records, options = {})
+
+ records = [*export_records]
- raise ArgumentError.new('Please supply array of records to export') unless records.is_a? Array
+
+ puts records, records.inspect
+ unless(records && records.size > 0)
+ logger.warn("No objects supplied for export")
+ return
+ end
+
first = records[0]
- return unless(first.is_a?(ActiveRecord::Base))
-
+ raise ArgumentError.new('Please supply set of ActiveRecord objects to export') unless(first.is_a?(ActiveRecord::Base))
+
f = options[:filename] || filename()
@text_delim = options[:text_delim] if(options[:text_delim])
File.open(f, "w") do |csv|
@@ -116,30 +125,30 @@
assignments.each {|x| csv << escape(r.send(x)) << Delimiters::csv_delim }
# done records basic attributes now deal with associations
#assoc_work_list.each do |op_type|
- # details_mgr.get_operators(op_type).each do |operator|
+ # details_mgr.get_operators(op_type).each do |operator|
assoc_operators.each do |operator|
- assoc_object = r.send(operator)
+ assoc_object = r.send(operator)
- if(assoc_object.is_a?ActiveRecord::Base)
- column_text = record_to_column(assoc_object) # belongs_to or has_one
+ if(assoc_object.is_a?ActiveRecord::Base)
+ column_text = record_to_column(assoc_object) # belongs_to or has_one
# TODO -ColumnPacker class shared between excel/csv
- csv << "#{@text_delim}#{column_text}#{@text_delim}" << Delimiters::csv_delim
- #csv << record_to_csv(r)
+ csv << "#{@text_delim}#{column_text}#{@text_delim}" << Delimiters::csv_delim
+ #csv << record_to_csv(r)
- elsif(assoc_object.is_a? Array)
- items_to_s = assoc_object.collect {|x| record_to_column(x) }
+ elsif(assoc_object.is_a? Array)
+ items_to_s = assoc_object.collect {|x| record_to_column(x) }
- # create a single column
- csv << "#{@text_delim}#{items_to_s.join(Delimiters::multi_assoc_delim)}#{@text_delim}" << Delimiters::csv_delim
+ # create a single column
+ csv << "#{@text_delim}#{items_to_s.join(Delimiters::multi_assoc_delim)}#{@text_delim}" << Delimiters::csv_delim
- else
- csv << Delimiters::csv_delim
- end
+ else
+ csv << Delimiters::csv_delim
+ end
#end
end
csv << Delimiters::eol # next record