lib/csv_rails/array.rb in csv_rails-0.6.0 vs lib/csv_rails/array.rb in csv_rails-0.6.1

- old
+ new

@@ -9,11 +9,13 @@ # * <tt>:fields</tt> - target field names # * <tt>:header</tt> - header # * <tt>:without_header</tt> - total_count # * <tt>:encoding</tt> - encoding # * <tt>:i18n_scope</tt> - i18n scope - def to_csv(opts={}) + + def separated_values(sep, opts) + row_sep = opts.delete(:row_sep) || :auto klass = first.class fields = if opts[:fields] opts.delete(:fields) elsif klass.respond_to?(:csv_fields) klass.csv_fields @@ -31,15 +33,23 @@ fields.map{|f| defaults = scopes.map{|s| "#{s}.#{f}".to_sym }.push(f.to_s) I18n.t(defaults.shift, :default => defaults) } end - csv = CSV.generate do |_csv| + csv = CSV.generate(:col_sep => sep, :row_sep => row_sep) do |_csv| _csv << header if header && !opts[:without_header] each do |element| _csv << element.to_csv_ary(fields, opts) end end opts[:encoding] ? csv.encode(opts[:encoding]) : csv + end + + def to_csv(opts={}) + separated_values(",", opts) + end + + def to_tsv(opts={}) + separated_values("\t", opts) end end end