-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRakefile
57 lines (51 loc) · 1.29 KB
/
Rakefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# Rakefile
require "./app"
require "csv"
require "json"
require "sinatra/activerecord/rake"
class CSVImporter
def import_all!
self.import 'routes.txt', 'Route'
end
def import(file_name, class_name)
puts "Importing #{class_name}s..."
klass = Object::const_get( class_name )
klass.delete_all
count = -1
headings = []
CSV.foreach("./gtfs/#{file_name}") do |row|
count += 1
if count == 0
headings = row
next
end
obj = klass.new
row.each_with_index do |value, i|
obj[ headings[i] ] = value
end
obj.save!
end
puts "#{count} #{class_name}s imported!"
end
end
namespace :hsr do
task :import do
i = CSVImporter.new
i.import_all!
# first_row = true
# headings = []
# CSV.foreach('./gtfs/routes.txt') do |row|
# if first_row
# first_row = false
# headings = row
# next
# end
# route = Route.new
# row.each_with_index do |value, i|
# route[ headings[i] ] = value
# end
# route.save
# puts route
# end
end
end