module Smartdict::Storage::Seeder

Public Instance Methods

seed!() click to toggle source
# File lib/smartdict/storage/seeder.rb, line 6
def seed!
  seed_files.each do |csv_file|
    seed_from(csv_file)
  end
end

Private Instance Methods

csv_file_to_model(csv_file) click to toggle source
# File lib/smartdict/storage/seeder.rb, line 30
def csv_file_to_model(csv_file)
  plural_name = File.basename(csv_file).gsub(/\.csv$/, "")
  model_name = plural_name.classify
  Smartdict::Models.const_get(model_name)
end
seed_files() click to toggle source
# File lib/smartdict/storage/seeder.rb, line 15
def seed_files
  seeds_dir = File.expand_path('../seeds', __FILE__)
  Dir["#{seeds_dir}/*.csv"]
end
seed_from(csv_file) click to toggle source
# File lib/smartdict/storage/seeder.rb, line 20
def seed_from(csv_file)
  model = csv_file_to_model(csv_file)
  csv = CSV.open(csv_file, 'r')
  headers = csv.shift
  while(row = csv.shift and not row.empty?)
    attrs = Hash[headers.zip(row)]
    model.create(attrs)
  end
end