Sha256: 42b2f4fc30ae2d33a4a61b7b7723b978b1861afa1cb648821aa367ac89ea5593

Contents?: true

Size: 800 Bytes

Versions: 2

Compression:

Stored size: 800 Bytes

Contents

require 'csv'
module CsvRails
  module Array
    def self.included(base)
      base.send(:remove_method, :to_csv)
      base.send(:include, CsvRails::Array::InstanceMethods)
    end

    module InstanceMethods
      def to_csv(opts={})
        return "" if length < 1
        first = self.first
        fields = opts.delete(:fields) || first.class.attribute_names
        CSV.generate do |csv|
          unless opts[:without_header]
            csv << if first.class.respond_to?(:human_attribute_name)
                     fields.map{|f| first.class.human_attribute_name(f) }
                   else
                     fields
                   end
          end
          each do |element|
             csv << element.to_csv_ary(fields, opts)
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
csv_rails-0.4.0 lib/csv_rails/array.rb
csv_rails-0.3.0 lib/csv_rails/array.rb