Class: CsvToObject::CsvToObject
- Inherits:
-
Object
- Object
- CsvToObject::CsvToObject
- Defined in:
- lib/csv_to_object.rb
Instance Method Summary collapse
-
#initialize(input_path) ⇒ CsvToObject
constructor
Requires the path to the csv file.
-
#to_objects ⇒ Object
Converts the data lines of the csv file to objects.
Constructor Details
#initialize(input_path) ⇒ CsvToObject
Requires the path to the csv file.
9 10 11 |
# File 'lib/csv_to_object.rb', line 9 def initialize(input_path) @input_path = File.open(input_path) end |
Instance Method Details
#to_objects ⇒ Object
Converts the data lines of the csv file to objects. Objects have the class of the name of the csv file. The first line of the csv file defines the attribute names for the data lines.
person.csv => [person objects]
attribute names are downcased and have spaces replaced with _.
attribute names are strings.
19 20 21 22 23 24 25 26 |
# File 'lib/csv_to_object.rb', line 19 def to_objects objects = [] file = File.open(@input_path) CSV.table(file, {header_converters: header_converter}).each do |row| objects << new_object(row.to_hash) end objects end |