Sha256: 587963cca21bd386c290297b13a0617a8947fd6bcd1c213e845d8978dbb6f78b

Contents?: true

Size: 615 Bytes

Versions: 1

Compression:

Stored size: 615 Bytes

Contents

require "csv_to_object/version"
require 'CSV'

module CsvToObject
  class CsvToObject

    def initialize(input_path)
      @input = File.open(input_path)
    end
    
    def to_objects
      objects = []
      CSV.table(@input).each do |row|
        objects << new_object(row.to_hash)
      end
      objects
    end
    
    private
        
    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-0.0.1 lib/csv_to_object.rb