class Seedfile

Attributes

model[RW]
path[RW]

Public Class Methods

new(options) click to toggle source
# File lib/seedfile.rb, line 11
def initialize(options)
  @path = options[:path]
  @model = options[:model]
  check_for_errors
end

Public Instance Methods

seed() click to toggle source
# File lib/seedfile.rb, line 17
def seed
  CSV.foreach(@path, headers: true, header_converters: :symbol) do |line|
    @model.create(line.to_hash)
  end
end

Private Instance Methods

check_for_errors() click to toggle source
# File lib/seedfile.rb, line 25
def check_for_errors
  raise FileNotFoundError unless File.exist?(@path)
  raise InvalidModelError unless @model.superclass == ActiveRecord::Base
end