Sha256: b26dee3c4a7b16fd7969455e1bc8896910c0822e7eaf186484d4974835bbc95c
Contents?: true
Size: 1.19 KB
Versions: 1
Compression:
Stored size: 1.19 KB
Contents
#coding: utf-8 require "csv_to_object/version" require 'csv' module CsvToObject class CsvToObject # Requires the path to the csv file. def initialize(input_path) @input_path = File.open(input_path) end # 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. def to_objects objects = [] file = File.open(@input_path) CSV.table(file, {header_converters: header_converter, encoding: "ISO-8859-1"}).each do |row| objects << new_object(row.to_hash) end objects end private def header_converter lambda { |h| h.encode(CSV::ConverterEncoding).downcase.gsub(/\s+/, "_") } end def new_object(attrs) @object ? @object.new(attrs) : object_to_create().new(attrs) end def object_to_create() class_name = File.basename(@input_path).gsub('.csv','').capitalize @object = Object::const_get(class_name) end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
csv_to_object-1.0.2 | lib/csv_to_object.rb |