Sha256: f87354a4413c6742d3183cd99917727be3eedd21d64a942019ac34b1ccea5455
Contents?: true
Size: 1.31 KB
Versions: 1
Compression:
Stored size: 1.31 KB
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 # ==== Options # * <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={}) fields = opts[:fields] header = if opts[:header] opts.delete(:header) elsif (klass = first.class).respond_to?(:csv_fields) klass.csv_fields else scopes = ['csv_rails'] scopes << opts[:i18n_scope] if opts[:i18n_scope] fields.map{|f| defaults = scopes.map{|s| "#{s}.#{f}".to_sym }.push(f.to_s) I18n.t(defaults.shift, :default => defaults) } if fields end csv = CSV.generate 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 end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
csv_rails-0.5.2 | lib/csv_rails/array.rb |