Skip to content

Upgrading from SmarterCSV 1.x to 2.x

Tilo edited this page Sep 18, 2023 · 10 revisions

SmarterCSV 2.x is trying to keep most of the defaults from version 1.2.3

NOTE: we're now at 1.9.0. This page needs to be updated.

Some options changed, some options from 1.x are discontinued with different ways to do things, so read on..

No more automatic Conversion of Numbers

Smarter CSV 1.x was automatically converting CSV column data which had strings with numerical data into Ruby numbers. That's pretty cool, but might not be safe in some cases - so it's now an optional transformation.

If you have strong opinions about this, please contact me via an issue.

Discontinued 1.x Options - no longer available in 2.x

In SmarterCSV 1.x there have been a lot of 1-offs and feature creep around these options, and going forward we'll have a simpler, but more flexible way to address these features.

Instead of the following options, there will be a new and more flexible way to transform the header fields, validate header fields, as well as transform and validate the fields in each line of the CSV.

Deprecated Options:

| Option                      | Default  |  Explanation                                                                         |
---------------------------------------------------------------------------------------------------------------------------------
| :key_mapping                |   nil    | a hash which maps headers from the CSV file to keys in the result hash               |
| :required_headers           |   nil    | An array. Eacn of the given headers must be present after header manipulation,       |
|                             |          | or an exception is raised   No validation if nil is given.                           |
| :remove_unmapped_keys       |   false  | when using :key_mapping option, should non-mapped keys / columns be removed?         |
| :downcase_header            |   true   | downcase all column headers                                                          |
| :strings_as_keys            |   false  | use strings instead of symbols as the keys in the result hashes                      |
| :strip_whitespace           |   true   | remove whitespace before/after values and headers                                    |
| :keep_original_headers      |   false  | keep the original headers from the CSV-file as-is.                                   |
|                             |          | Disables other flags manipulating the header fields.                                 |
| :strip_chars_from_headers   |   nil    | RegExp to remove extraneous characters from the header line (e.g. if headers are quoted) |
---------------------------------------------------------------------------------------------------------------------------------
| :value_converters           |   nil    | supply a hash of :header => KlassName; the class needs to implement self.convert(val)|
| :remove_empty_values        |   true   | remove values which have nil or empty strings as values                              |
| :remove_zero_values         |   true   | remove values which have a numeric value equal to zero / 0                           |
| :remove_values_matching     |   nil    | removes key/value pairs if value matches given regular expressions. e.g.:            |
|                             |          | /^\$0\.0+$/ to match $0.00 , or /^#VALUE!$/ to match errors in Excel spreadsheets    |
| :convert_values_to_numeric  |   true   | converts strings containing Integers or Floats to the appropriate class              |
|                             |          |      also accepts either {:except => [:key1,:key2]} or {:only => :key3}              |
---------------------------------------------------------------------------------------------------------------------------------

Transformations and Validations

SmarterCSV 2.x has a couple of transformations and validations which can be customized, either by using pre-defined ones, or you can even create your own Procs to do custom validations or transformations.

Transmogrify my Data contains more details about this.

SmarterCSV 2.x Defaults

  {
    header_transformations: [:keys_as_symbols],
    header_validations:   [ :unique_headers ],
    data_transformations: [ :replace_blank_with_nil ],
    data_validations: [],
    hash_transformations: [ :strip_spaces, :remove_blank_values ],
    hash_validations: []
  }

SmarterCSV 1.x Backwards Compatible Mode

  {
     header_transformations: [:keys_as_symbols],
     header_validations: [:unique_headers],
     data_transformations: [ :replace_blank_with_nil ],
     data_validations: [],
     hash_transformations: [:strip_spaces, :remove_blank_values, :convert_values_to_numeric],
     hash_validations: []
  }