Sha256: 7bd57206578cc75dc67d90a246671415e2305f03e27eec21b14e65347cd13b2e

Contents?: true

Size: 1.82 KB

Versions: 5

Compression:

Stored size: 1.82 KB

Contents

# CsvToObject

Given the path to a CSV file, will return a collection of objects. The csv file name must match the name of an object the gem can instantiate. The first line of the csv file must define attribute names. The following lines contain the data that will be assigned to those attributes.

## Installation

Add this line to your application's Gemfile:

    gem 'csv_to_object'

And then execute:

    $ bundle

Or install it yourself as:

    $ gem install csv_to_object

## Usage

Given a file person.csv with the contents

  `id,first_name,last_name`  
  `0,Lindsey,Austino`  
  `1,Dodie,Egnor`

  ```ruby
    csv_path = 'tmp/person.csv'
    CsvToObject::CsvToObject.new(csv.path).to_objects =>[#<Person:0x000001021a8148 @id=0, @first_name="Lindsey", @last_name="Austino">, #<Person:0x000001021a8030 @id=1, @first_name="Dodie", @last_name="Egnor">]
  ```

### Attribute Names

CSV_to_object does not use the default header converter options provided by Ruby's CSV module. When converting the first line of the csv file into attribute names, the following conversions are applied:

* Encoded using CSV::CharacterEncoding
* Downcased
* Spaces are replaced with underscores

If this behavior is not desired, overwrite the `header_converter` method of `CsvToObject`.

When passed to the target object.new() method, attribute names are strings, not symbols. For example, the instantiation of the Person object above would be:

  ```ruby
    Person.new({"id" => 0, "first_name" => "Lindsey", "last_name" => "Austino"})
  ```
So if you are looking for specific hash keys in `initialize()`, use strings not symbols.

## Contributing

1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Added some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
csv_to_object-1.0.4 README.md
csv_to_object-1.0.3 README.md
csv_to_object-1.0.2 README.md
csv_to_object-1.0.1 README.md
csv_to_object-1.0.0 README.md