Class: Array
- Inherits:
-
Object
- Object
- Array
- Defined in:
- lib/to_deep_csv/array.rb
Instance Method Summary collapse
-
#to_deep_csv(options = {}) ⇒ Object
to_csv for Array<Array>, Array<Hash>, Array<ActiveRecord::Base>, ActiveRecord::Relation etc.
Instance Method Details
#to_deep_csv(options = {}) ⇒ Object
to_csv for Array<Array>, Array<Hash>, Array<ActiveRecord::Base>, ActiveRecord::Relation etc.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/to_deep_csv/array.rb', line 19 def to_deep_csv( = {}) # true if Hash like columns_by_first = self.first.try(:attributes).try(:keys) || self.first.try(:keys) columns = .delete(:columns) || columns_by_first CSV.generate("", ) do |csv| csv << columns if columns && [:headers] self.each do |record| csv << if columns_by_first # Hash like columns.map do |column| Array(column).reduce(record) {|target, key| target.try(key) || target[key] } end else # Array like record end end end end |