Created
November 13, 2011 17:22
-
-
Save masom/1362361 to your computer and use it in GitHub Desktop.
Import from Canadian food db to SQL using ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#http://erikonrails.snowedin.net/?p=212 | |
desc "Imports a CSV file into an ActiveRecord table" | |
task :csv_model_import, :source, :model, :needs => :environment do |task,args| | |
def import_csv(classname, options = {}) | |
lines = File.new(options[:filename]).readlines | |
header = lines.shift.strip | |
keys = header.split(',') | |
model = Module.const_get(classname) | |
lines.each do |line| | |
params = {} | |
values = line.strip.split(',') | |
keys.each_with_index do |key,i| | |
continue unless options[:fields].has_key? key | |
params[options[:fields][key]] = values[i] | |
end | |
model.create(params) | |
end | |
end | |
if args[:source] == 'CND' | |
#Model list is ordered by import. | |
models = { | |
'FoodGroup' => { | |
:filename => 'FOOD_GRP.txt', | |
:fields => { | |
:FD_GRP_ID => :id, | |
:FD_GRP_NME => :name, | |
:FD_GRP_COD => :code | |
} | |
}, | |
'FoodSource' => { | |
:filename => 'FOOD_SRC.txt', | |
:fields => { | |
:FD_SRC_ID => :id, | |
:FD_SRC_COD => :code, | |
:FD_SRC_NME => :name | |
} | |
}, | |
'FoodMeasure' => { | |
:filename => 'MEASURE.txt', | |
:fields => { | |
:MSR_ID => :id, | |
:MSR_NME => :name | |
} | |
}, | |
'FoodNutrient' => { | |
:filename => 'NT_NM.txt', | |
:fields => { | |
:NT_ID => :id, | |
:NT_COD => :code, | |
:NT_SYM => :symbol, | |
:UNIT => :unit, | |
:NT_NME => :name, | |
:TAGNAME => :tagname | |
} | |
}, | |
'FoodYield' => { | |
:filename => 'YLD_NM.txt', | |
:fields => { | |
:YLD_ID => :id, | |
:YLD_NAME => :name | |
} | |
}, | |
'FoodRefuse' => { | |
:filename => 'REFU_NM.txt', | |
:fields => { | |
:REFUSE_ID => :id, | |
:REFUSE_NME => :name | |
} | |
}, | |
'FoodNutrientSource' => { | |
:filename => 'NT_SRC.txt', | |
:fields => { | |
:NT_SRC_ID => :id, | |
:NT_SRC_COD => :code, | |
:NT_SRC_NME => :name | |
} | |
}, | |
'FoodNutrient' => { | |
:filename => 'NT_NM.txt', | |
:fields => { | |
:NT_ID => :id, | |
:NT_COD => :code, | |
:NT_SYM => :symbol, | |
:UNIT => :unit, | |
:NT_NME => :name, | |
:NT_TAGNAME => :tagname | |
} | |
}, | |
#These depends on Foods and the previous enumerations | |
'Foods' => { | |
:filename => 'FOOD_NM.txt', | |
:fields => { | |
:FD_ID => :id, | |
:FD_CODE => :code, | |
:FD_GRP_ID => :food_group_id, | |
:FD_SRC_ID => :food_source_id, | |
:L_FD_NME => :name | |
} | |
}, | |
'FoodNutrientAmount' => 'NT_AMT.txt', | |
'FoodsYields' => 'YIELD.txt', | |
'FoodsRefuses' => 'REFUSE.txt', | |
'FoodConversion' => 'CONV_FAC.txt' | |
} | |
if args[:model] == 'all' | |
models.each do |model,filename| | |
import_csv model, options | |
end | |
else | |
filename = models[args[:model]] | |
import_csv args[:model], options | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment