-
Notifications
You must be signed in to change notification settings - Fork 1
/
Rakefile
83 lines (73 loc) · 2.66 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
require File.expand_path('../config/application', __FILE__)
require 'csv'
DisasterManagement::Application.load_tasks
namespace :bootstrap do
options = {:headers => false,
:converters => [ :numeric ]}
desc "Import districts"
task :district => :environment do
District.destroy_all
CSV.open(File.expand_path('../data/district.csv', __FILE__), "r", options) do |csv|
csv.find_all do |row|
lat, lng = Geocoder.coordinates("#{row[0]}")
District.create(:name => row[0], :lat => lat, :lng => lng)
end
end
end
desc "Import Municipalities"
task :municipalities => :environment do
Municipality.destroy_all
CSV.open(File.expand_path('../data/municipalities.csv', __FILE__), "r", options) do |csv|
csv.find_all do |row|
lat, lng = Geocoder.coordinates("#{row[0]},#{row[1]}")
Municipality.create(:name => row[0], :district => District.find_by_name(row[1]), :lat => lat, :lng => lng)
end
end
end
desc "Import Disaster"
task :disaster => :environment do
Disaster.destroy_all
CSV.open(File.expand_path('../data/disaster.csv', __FILE__), "r", options) do |csv|
csv.find_all do |row|
Disaster.create(:name => row[0])
end
end
end
desc "Import Resources"
task :resources => :environment do
Resource.destroy_all
CSV.open(File.expand_path('../data/resources.csv', __FILE__), "r", options) do |csv|
csv.find_all do |row|
Resource.create(:name => row[0])
end
end
end
desc "Import Resource Allocation"
task :allocation => :environment do
ResourceAllocation.destroy_all
CSV.open(File.expand_path('../data/resource_allocation.csv', __FILE__), "r", options) do |csv|
csv.find_all do |row|
ResourceAllocation.create(:disaster => Disaster.find_by_name(row[0]), :resource => Resource.find_by_name(row[1]), :district => District.find_by_name(row[2]), :quantity => row[3])
end
end
end
desc "Import Resource Availability"
task :availability => :environment do
ResourceAvailabilty.destroy_all
CSV.open(File.expand_path('../data/resource_availability.csv', __FILE__), "r", options) do |csv|
csv.find_all do |row|
ResourceAvailabilty.create(:municipality => Municipality.find_by_name(row[0]), :resource => Resource.find_by_name(row[1]), :availability => row[2])
end
end
end
desc "Import all"
task :all => :environment do
Rake::Task['bootstrap:district'].execute
Rake::Task['bootstrap:municipalities'].execute
Rake::Task['bootstrap:disaster'].execute
Rake::Task['bootstrap:allocation'].execute
Rake::Task['bootstrap:availability'].execute
end
end