CsvSerializer

Adds easy CSV generation functionality to ActiveRecord.

Usage

Suppose Person model is defined.

class Person < ApplicationRecord
  # In schema.rb, columns are defined as below definition.
  # create_table "people", force: :cascade do |t|
  #   t.string "name"
  #   t.integer "age"
  #   t.integer "tall"
  #   t.integer "weight"
  #   t.datetime "created_at", precision: 6, null: false
  #   t.datetime "updated_at", precision: 6, null: false
  # end
end

to_csv returns csv as string.

Person.to_csv
# => id,name,age,tall,weight,created_at,updated_at
#    1,sample1,1,128,34,2020-01-01 10:02:39 UTC,2020-01-01 11:02:39 UTC
#    2,sample2,2,130,32,2020-01-01 12:02:39 UTC,2020-01-01 14:02:39 UTC

If attribute names is passed, CSV string only contains the specified attributes will be returned.

Person.to_csv(:id, :name)
# => id,name
#    1,sample1
#    2,sample2

Containing the value gained by processing the record is supported.

Person.to_csv(
  "long name": ->(user) { user.name * 2 },
  "short name": ->(user) { user.name[-2..] }
)
# => long name,short name
#    sample1sample1,e1
#    sample2sample2,e2

Same named columns can be specified with arrays of a column name and a producer function.

Person.all.to_csv(
  ["long name", ->(user) { user.name * 2 }],
  ["long name", ->(user) { user.name * 3 }]
)
# => Long name,Long name
#    sample1sample1,sample1sample1sample1

Output to files

io = Tempfile.new
Person.all.to_csv_stream(io)
# Below lines are written to tempfile.
# id,name,age,tall,weight,created_at,updated_at
# 1,sample1,1,128,34,2020-01-01 10:02:39 UTC,2020-01-01 11:02:39 UTC
# 2,sample2,2,130,32,2020-01-01 12:02:39 UTC,2020-01-01 14:02:39 UTC

Installation

Add this line to your application's Gemfile:

gem 'csv_serializer'

And then execute:

$ bundle

Or install it yourself as:

$ gem install csv_serializer

Contributing

Contribution directions go here.

License

The gem is available as open source under the terms of the MIT License.