Class: Apiify::CsvImporter
- Inherits:
-
Object
- Object
- Apiify::CsvImporter
- Defined in:
- lib/apiify/csv_importer.rb
Instance Method Summary collapse
- #create_rake_file ⇒ Object
- #import(csv_path) ⇒ Object
- #output_message(csv_path) ⇒ Object
- #run_rake_task(csv_path) ⇒ Object
Instance Method Details
#create_rake_file ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/apiify/csv_importer.rb', line 9 def create_rake_file File.open("./lib/tasks/importer.rake", 'w') do |f| f.write("require 'CSV' task :import_csv, [:file_path] => :environment do |t, args| file_name = args[:file_path].split('/').last.split('.').first model_name = file_name.capitalize.constantize table = CSV.table(args[:file_path]) table.each do |row| model_name.create!(row.to_hash) end end") end end |
#import(csv_path) ⇒ Object
3 4 5 6 7 |
# File 'lib/apiify/csv_importer.rb', line 3 def import(csv_path) create_rake_file run_rake_task(csv_path) (csv_path) end |
#output_message(csv_path) ⇒ Object
27 28 29 30 |
# File 'lib/apiify/csv_importer.rb', line 27 def (csv_path) rows = CSV.table("#{csv_path}").length puts "#{rows} records imported successfully." end |
#run_rake_task(csv_path) ⇒ Object
23 24 25 |
# File 'lib/apiify/csv_importer.rb', line 23 def run_rake_task(csv_path) system("bin/rake import_csv\[#{csv_path}\]") end |