0.0
No commit activity in last 3 years
No release in over 3 years
CsvCreator provides nice, configurable interface which will generate CSV file for you, from collection you want (database or array with non-db related objects).
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.10
~> 5.7
~> 0.10.1
~> 10.0

Runtime

 Project Readme

CsvCreator

CsvCreator provides nice, configurable interface which will generate CSV file for you, from collection you want (database or array with non-db related objects).

Installation

Add this line to your application's Gemfile:

gem 'csv_creator'

And then execute:

$ bundle

Or install it yourself as:

$ gem install csv_creator

Usage

Basic usage with default options (see below) is just by calling create method on CsvCreator module. There is also an alias method generate. It does the same thing and takes the same arguments.

CsvCreator.create(my_collection)

Rails

Example application that shows how this gem can be used in Rails with Activerecord (works with Mongoid as well) is under this link.

Options

Options can be passed as second argument to the create method. The default options hash looks like this.

{
  csv: { force_quotes: true, headers: true },
  only: nil,
  without: nil,
  order: [],
  order_direction: :asc,
  translate: {},
  callbacks: {}
}

Options explanation

  • csv - In csv key you can pass all options that you would usually pass to the CSV.generate. Yoo can find more about them under this link. Look for "DEFAULT_OPTIONS".
CsvCreator.create(my_collection, { csv: { force_quotes: false } })
  • only - It's self explanatory. By passing an array of field names you will get in your CSV data only for that fields.
CsvCreator.create(my_collection, { only: [:id, :name] })
  • without - Like above, but this time, fields that are in the array passed under this key will be skipped during generation process.
CsvCreator.create(my_collection, { without: [:id, :name] })
  • order - Makes sense only for collections that are not from database. Obviously it's way better to sort collection with use of database queries. But if you want, CsvCreator can sort given collection for you. Under the hood there is simple sort_by method.
CsvCreator.create(my_collection, { order: [:name] })
  • order_direction - If you defined some fields that you want to order by your collection, you can manipulate direction.
CsvCreator.create(my_collection, { order: [:name], order_direction: :desc })
  • translate - Useful if you want to somehow change how the column header will be displayed in the CSV document. Very useful for language translations.
CsvCreator.create(my_collection, { translate: { name: 'Translated Name Field' })
  • callbacks - Callbacks can help you with modifying result for each row exactly for the CSV document. For example by formatting date time field or some other calculations.
options = { callbacks: { birth_date: ->(person) { person.birth_date.strftime('%m/%d/%Y') } } }
CsvCreator.create(my_collection, options)

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/dsawa/csv_creator.

License

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