Sha256: 438f08b4e6e6566497d34515149be44f1113e32e1376c09a1a8846bda0bc6450

Contents?: true

Size: 878 Bytes

Versions: 4

Compression:

Stored size: 878 Bytes

Contents

require "csv"
# for compatibility with Rails 2
require 'fastercsv' if CSV.const_defined?(:Reader)

module MakeExportable #:nodoc:
  class CSV < ExportableFormat #:nodoc:

    cattr_accessor :csv_type

    self.reference = :csv
    self.name      = 'CSV'
    self.register_format
    self.csv_type = ::CSV.const_defined?(:Reader) ? FasterCSV : ::CSV

    attr_accessor :data_set, :data_headers

    def initialize(data_set, data_headers=[])
      self.long      = 'Comma-separated (CSV)'
      self.mime_type = 'text/csv; charset=utf-8;'
      self.data_set = data_set
      self.data_headers = data_headers
    end


    def generate
      generate_header_option(data_headers)
      @@csv_type.generate do |csv|
        csv << data_headers.map {|h| sanitize(h.humanize.titleize)} unless data_headers.blank?
        data_set.each {|row| csv << row }
      end
    end
  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
make_exportable-1.1.0 lib/make_exportable/exportable_formats/csv.rb
make_exportable-1.0.3 lib/make_exportable/exportable_formats/csv.rb
make_exportable-1.0.2 lib/make_exportable/exportable_formats/csv.rb
make_exportable-1.0.1 lib/make_exportable/exportable_formats/csv.rb