Csvash automates your CSV extraction by mapping its headers with the columns contents and turning them into hashes that can be easily converted into ruby models.
Add this line to your application's Gemfile:
gem 'csvash'
And then execute:
$ bundle
Or install it yourself as:
$ gem install csvash
First of all you have to require it in your file:
require 'csvash'
To get a collection of hashes containing the csv data you can call:
Csvash.hashify '/path/of/your/file.csv'
Note: You can use modelify_and_export
or modelify_and_import
methods to generate or parse csv files respectively.
In order to export, you can pass the desired path and CSV filename. Finally, you can pass a collection of already filled objects from a specific class you can pass the array:
Csvash.modelify_and_export '/path/to/file.csv', collection
Where collection is an array of User objects, for example
However, to import from a csv file you can pass the path where the file is allocated and a class, as usual:
Csvash.modelify_and_import '/path/of/your/file.csv', User
* The path and its directories are created automatically
##Options
There are some behavior options that can be changed with its initializer. To create it just type:
$ rails generate initializer csvash
These are the avaiable options
# If set to true it will only retain fields that matches the class to be filled. Default is false.
# Csvash.mass_assignment_safe = true
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Added some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request