Module: CsvRails::Array
- Defined in:
- lib/csv_rails/array.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#separated_values(sep, opts) ⇒ Object
Options *
:fields
- target field names *:header
- header *:without_header
- total_count *:encoding
- encoding *:i18n_scope
- i18n scope. - #to_csv(opts = {}) ⇒ Object
- #to_tsv(opts = {}) ⇒ Object
Class Method Details
.included(base) ⇒ Object
4 5 6 |
# File 'lib/csv_rails/array.rb', line 4 def self.included(base) base.send(:remove_method, :to_csv) end |
Instance Method Details
#separated_values(sep, opts) ⇒ Object
Options
-
:fields
- target field names -
:header
- header -
:without_header
- total_count -
:encoding
- encoding -
:i18n_scope
- i18n scope
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/csv_rails/array.rb', line 15 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 else [] end header = if opts[:header] opts.delete(:header) elsif klass.respond_to?(:csv_header) klass.csv_header(fields, opts.delete(:i18n_scope)) 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) } end 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 |
#to_csv(opts = {}) ⇒ Object
47 48 49 |
# File 'lib/csv_rails/array.rb', line 47 def to_csv(opts={}) separated_values(",", opts) end |
#to_tsv(opts = {}) ⇒ Object
51 52 53 |
# File 'lib/csv_rails/array.rb', line 51 def to_tsv(opts={}) separated_values("\t", opts) end |