Skip to content

Getting Started

Tilo edited this page Feb 13, 2022 · 6 revisions

Requirements

SmarterCSV supports Ruby >= 2.2, and is not tied to a specific Rails version.

Please take note that Ruby 2.2 EOL date is scheduled for 2018-03-31

Installation

   gem install smarter_csv

Features

Now SmarterCSV 2.0 is out, and strives to keep the same features, but using a different implementation which allows you more control when you need to handle special cases. Becaues of this, some of the options from version 1.x are no longer supported. Alternative solutions can be found in the Upgrading guide.

SmarterCSV 2.x has lots of features:

  • able to process large CSV-files

  • able to chunk the input from the CSV file to avoid loading the whole CSV file into memory

  • return a Hash for each line of the CSV file, so we can quickly use the results for either creating MongoDB or ActiveRecord entries, or further processing with Resque

  • able to pass a block to the process method, so data from the CSV file can be directly processed (e.g. Resque.enqueue )

  • allows to have a bit more flexible input format, where comments are possible, and col_sep,row_sep can be set to any character sequence, including control characters.

  • able to transmogrify CSV column names to the Hash keys of your choice (see: header_transformations)

  • able to ignore unwanted CSV columns and exclude them from the resulting hash (just map them to nil)

  • able to do validations on the resulting keys computed from CSV column names (see: header_validations)

  • able to do data transformations for all or select columns (see: data_transformations, hash_transformations)

  • able to do data validations (TO BE IMPLEMENTED SOON)

  • able to eliminate key/value pairs with blank, or nil values from the result hashes.

  • you can use the transformations to implement any custom behavior by passing-in one or more Procs.

NOTE: This Gem is only for importing CSV files - writing of CSV files is not supported at this time, but writing is on the feature list for the future..

Default Behavior vs Customization

SmarterCSV was designed with the use cases in mind that you want to use the imported data to either update a database record, or pass the data on to a background worker. It's default behavior is to change the headers of a CSV file into symbols, which are then used in a hash that gets constructed for each line of the CSV file. This default behavior can be changed and customized.

Next: The Basics