require 'csv' namespace :import do desc "Import Australian Food Classification Data from a CSV file" task australian_food_classification_data: :environment do HEADER_MAP = { "Public Food Key" => :public_food_key, "Classification" => :classification, "Food Name" => :food_name, "Energy with dietary fibre, equated \n(kJ)" => :energy_with_dietary_fibre_equated_kj, "Energy, without dietary fibre, equated \n(kJ)" => :energy_without_dietary_fibre_equated_kj, "Moisture (water) \n(g)" => :moisture_water_g, "Protein \n(g)" => :protein_g, "Nitrogen \n(g)" => :nitrogen_g, "Fat, total \n(g)" => :fat_total_g, "Ash \n(g)" => :ash_g, "Total dietary fibre \n(g)" => :total_dietary_fibre_g, "Alcohol \n(g)" => :alcohol_g, "Fructose \n(g)" => :fructose_g, "Glucose \n(g)" => :glucose_g, "Sucrose\n(g)" => :sucrose_g, "Maltose \n(g)" => :maltose_g, "Lactose \n(g)" => :lactose_g, "Galactose \n(g)" => :galactose_g, "Maltotrios \n(g)" => :maltotrios_g, "Total sugars (g)" => :total_sugars_g, "Added sugars (g)" => :added_sugars_g, "Free sugars \n(g)" => :free_sugars_g, "Starch \n(g)" => :starch_g, "Dextrin \n(g)" => :dextrin_g, "Glycerol \n(g)" => :glycerol_g, "Glycogen \n(g)" => :glycogen_g, "Inulin \n(g)" => :inulin_g, "Erythritol \n(g)" => :erythritol_g, "Maltitol \n(g)" => :maltitol_g, "Mannitol \n(g)" => :mannitol_g, "Xylitol \n(g)" => :xylitol_g, "Maltodextrin (g)" => :maltodextrin_g, "Oligosaccharides \n(g)" => :oligosaccharides_g, "Polydextrose \n(g)" => :polydextrose_g, "Raffinose \n(g)" => :raffinose_g, "Stachyose \n(g)" => :stachyose_g, "Sorbitol \n(g)" => :sorbitol_g, "Resistant starch \n(g)" => :resistant_starch_g, "Available carbohydrate, without sugar alcohols \n(g)" => :available_carbohydrate_without_sugar_alcohols_g, "Available carbohydrate, with sugar alcohols \n(g)" => :available_carbohydrate_with_sugar_alcohols_g, "Acetic acid \n(g)" => :acetic_acid_g, "Citric acid \n(g)" => :citric_acid_g, "Fumaric acid \n(g)" => :fumaric_acid_g, "Lactic acid \n(g)" => :lactic_acid_g, "Malic acid\n (g)" => :malic_acid_g, "Oxalic acid \n(g)" => :oxalic_acid_g, "Propionic acid \n(g)" => :propionic_acid_g, "Quinic acid \n(g)" => :quinic_acid_g, "Shikimic acid \n(g)" => :shikimic_acid_g, "Succinic acid \n(g)" => :succinic_acid_g, "Tartaric acid \n(g)" => :tartaric_acid_g, "Aluminium (Al) \n(ug)" => :aluminium_al_ug, "Antimony (Sb) \n(ug)" => :antimony_sb_ug, "Arsenic (As) \n(ug)" => :arsenic_as_ug, "Cadmium (Cd) \n(ug)" => :cadmium_cd_ug, "Calcium (Ca) \n(mg)" => :calcium_ca_mg, "Chromium (Cr) \n(ug)" => :chromium_cr_ug, "Chloride (Cl) \n(mg)" => :chloride_cl_mg, "Cobalt (Co) \n(ug)" => :cobalt_co_ug, "Copper (Cu) \n(mg)" => :copper_cu_mg, "Fluoride (F) \n(ug)" => :fluoride_f_ug, "Iodine (I) \n(ug)" => :iodine_i_ug, "Iron (Fe) \n(mg)" => :iron_fe_mg, "Lead (Pb) \n(ug)" => :lead_pb_ug, "Magnesium (Mg) \n(mg)" => :magnesium_mg_mg, "Manganese (Mn) \n(mg)" => :manganese_mn_mg, "Mercury (Hg) \n(ug)" => :mercury_hg_ug, "Molybdenum (Mo) \n(ug)" => :molybdenum_mo_ug, "Nickel (Ni) \n(ug)" => :nickel_ni_ug, "Phosphorus (P) \n(mg)" => :phosphorus_p_mg, "Potassium (K) \n(mg)" => :potassium_k_mg, "Selenium (Se) \n(ug)" => :selenium_se_ug, "Sodium (Na) \n(mg)" => :sodium_na_mg, "Sulphur (S) \n(mg)" => :sulphur_s_mg, "Tin (Sn) \n(ug)" => :tin_sn_ug, "Zinc (Zn) \n(mg)" => :zinc_zn_mg, "Retinol (preformed vitamin A) \n(ug)" => :retinol_preformed_vitamin_a_ug, "Alpha-carotene \n(ug)" => :alpha_carotene_ug, "Beta-carotene \n(ug)" => :beta_carotene_ug, "Cryptoxanthin \n(ug)" => :cryptoxanthin_ug, "Beta-carotene equivalents (provitamin A) \n(ug)" => :beta_carotene_equivalents_provitamin_a_ug, "Vitamin A retinol equivalents \n(ug)" => :vitamin_a_retinol_equivalents_ug, "Lutein \n(ug)" => :lutein_ug, "Lycopene \n(ug)" => :lycopene_ug, "Xanthophyl \n(ug)" => :xanthophyl_ug, "Thiamin (B1) \n(mg)" => :thiamin_b1_mg, "Riboflavin (B2) \n(mg)" => :riboflavin_b2_mg, "Niacin (B3) \n(mg)" => :niacin_b3_mg, "Niacin derived from tryptophan \n(mg)" => :niacin_derived_from_tryptophan_mg, "Niacin derived equivalents \n(mg)" => :niacin_derived_equivalents_mg, "Pantothenic acid (B5) \n(mg)" => :pantothenic_acid_b5_mg, "Pyridoxine (B6) \n(mg)" => :pyridoxine_b6_mg, "Biotin (B7) \n(ug)" => :biotin_b7_ug, "Cobalamin (B12) \n(ug)" => :cobalamin_b12_ug, "Folate, natural \n(ug)" => :folate_natural_ug, "Folic acid \n(ug)" => :folic_acid_ug, "Total folates \n(ug)" => :total_folates_ug, "Dietary folate equivalents \n(ug)" => :dietary_folate_equivalents_ug, "Vitamin C \n(mg)" => :vitamin_c_mg, "Cholecalciferol (D3) \n(ug)" => :cholecalciferol_d3_ug, "Ergocalciferol (D2) \n(ug)" => :ergocalciferol_d2_ug, "25-hydroxy cholecalciferol (25-OH D3) \n(ug)" => :c25_hydroxy_cholecalciferol_25_oh_d3_ug, "25-hydroxy ergocalciferol (25-OH D2) \n(ug)" => :c25_hydroxy_ergocalciferol_25_oh_d2_ug, "Vitamin D3 equivalents \n(ug)" => :vitamin_d3_equivalents_ug, "Alpha tocopherol \n(mg)" => :alpha_tocopherol_mg, "Alpha tocotrienol \n(mg)" => :alpha_tocotrienol_mg, "Beta tocopherol \n(mg)" => :beta_tocopherol_mg, "Beta tocotrienol \n(mg)" => :beta_tocotrienol_mg, "Delta tocopherol \n(mg)" => :delta_tocopherol_mg, "Delta tocotrienol \n(mg)" => :delta_tocotrienol_mg, "Gamma tocopherol \n(mg)" => :gamma_tocopherol_mg, "Gamma tocotrienol \n(mg)" => :gamma_tocotrienol_mg, "Vitamin E \n(mg)" => :vitamin_e_mg, "C4 (%T)" => :c4_t, "C6 (%T)" => :c6_t, "C8 (%T)" => :c8_t, "C10 (%T)" => :c10_t, "C11 (%T)" => :c11_t, "C12 (%T)" => :c12_t, "C13 (%T)" => :c13_t, "C14 (%T)" => :c14_t, "C15 (%T)" => :c15_t, "C16 (%T)" => :c16_t, "C17 (%T)" => :c17_t, "C18 (%T)" => :c18_t, "C19 (%T)" => :c19_t, "C20 (%T)" => :c20_t, "C21 (%T)" => :c21_t, "C22 (%T)" => :c22_t, "C23 (%T)" => :c23_t, "C24 (%T)" => :c24_t, "Total saturated fatty acids, equated (%T)" => :total_saturated_fatty_acids_equated_t, "C10:1 (%T)" => :c10_1_t, "C12:1 (%T)" => :c12_1_t, "C14:1 (%T)" => :c14_1_t, "C15:1 (%T)" => :c15_1_t, "C16:1 (%T)" => :c16_1_t, "C17:1 (%T)" => :c17_1_t, "C18:1 (%T)" => :c18_1_t, "C18:1w5 (%T)" => :c18_1w5_t, "C18:1w6 (%T)" => :c18_1w6_t, "C18:1w7 (%T)" => :c18_1w7_t, "C18:1w9 (%T)" => :c18_1w9_t, "C20:1 (%T)" => :c20_1_t, "C20:1w9 (%T)" => :c20_1w9_t, "C20:1w13 (%T)" => :c20_1w13_t, "C20:1w11 (%T)" => :c20_1w11_t, "C22:1 (%T)" => :c22_1_t, "C22:1w9 (%T)" => :c22_1w9_t, "C22:1w11 (%T)" => :c22_1w11_t, "C24:1 (%T)" => :c24_1_t, "C24:1w9 (%T)" => :c24_1w9_t, "C24:1w11 (%T)" => :c24_1w11_t, "C24:1w13 (%T)" => :c24_1w13_t, "Total monounsaturated fatty acids, equated (%T)" => :total_monounsaturated_fatty_acids_equated_t, "C12:2 (%T)" => :c12_2_t, "C16:2w4 (%T)" => :c16_2w4_t, "C16:3 (%T)" => :c16_3_t, "C18:2w6 (%T)" => :c18_2w6_t, "C18:3w3 (%T)" => :c18_3w3_t, "C18:3w4 (%T)" => :c18_3w4_t, "C18:3w6 (%T)" => :c18_3w6_t, "C18:4w1 (%T)" => :c18_4w1_t, "C18:4w3 (%T)" => :c18_4w3_t, "C20:2 (%T)" => :c20_2_t, "C20:2w6 (%T)" => :c20_2w6_t, "C20:3 (%T)" => :c20_3_t, "C20:4 (%T)" => :c20_4_t, "C20:3w3 (%T)" => :c20_3w3_t, "C20:3w6 (%T)" => :c20_3w6_t, "C20:4w3 (%T)" => :c20_4w3_t, "C20:4w6 (%T)" => :c20_4w6_t, "C20:5w3 (%T)" => :c20_5w3_t, "C21:5w3 (%T)" => :c21_5w3_t, "C22:2 (%T)" => :c22_2_t, "C22:2w6 (%T)" => :c22_2w6_t, "C22:4w6 (%T)" => :c22_4w6_t, "C22:5w3 (%T)" => :c22_5w3_t, "C22:5w6 (%T)" => :c22_5w6_t, "C22:6w3 (%T)" => :c22_6w3_t, "Total polyunsaturated fatty acids, equated (%T)" => :total_polyunsaturated_fatty_acids_equated_t, "Total long chain omega 3 fatty acids, equated \n(%T)" => :total_long_chain_omega_3_fatty_acids_equated_t, "Total undifferentiated fatty acids \n(%T)" => :total_undifferentiated_fatty_acids_t, "Total trans fatty acids, imputed \n(%T)" => :total_trans_fatty_acids_imputed_t, "C4 (g)" => :c4_g, "C6 (g)" => :c6_g, "C8 (g)" => :c8_g, "C10 (g)" => :c10_g, "C11 (g)" => :c11_g, "C12 (g)" => :c12_g, "C13 (g)" => :c13_g, "C14 (g)" => :c14_g, "C15 (g)" => :c15_g, "C16 (g)" => :c16_g, "C17 (g)" => :c17_g, "C18 (g)" => :c18_g, "C19 (g)" => :c19_g, "C20 (g)" => :c20_g, "C21 (g)" => :c21_g, "C22 (g)" => :c22_g, "C23 (g)" => :c23_g, "C24 (g)" => :c24_g, "Total saturated fatty acids, equated \n(g)" => :total_saturated_fatty_acids_equated_g, "C10:1 (g)" => :c10_1_g, "C12:1 (g)" => :c12_1_g, "C14:1 (g)" => :c14_1_g, "C15:1 (g)" => :c15_1_g, "C16:1 (g)" => :c16_1_g, "C17:1 (g)" => :c17_1_g, "C18:1 (g)" => :c18_1_g, "C18:1w5 (mg)" => :c18_1w5_mg, "C18:1w6 (mg)" => :c18_1w6_mg, "C18:1w7 (g)" => :c18_1w7_g, "C18:1w9 (mg)" => :c18_1w9_mg, "C20:1 (g)" => :c20_1_g, "C20:1w9 (mg)" => :c20_1w9_mg, "C20:1w13 (mg)" => :c20_1w13_mg, "C20:1w11 (mg)" => :c20_1w11_mg, "C22:1 (g)" => :c22_1_g, "C22:1w9 (mg)" => :c22_1w9_mg, "C22:1w11 (mg)" => :c22_1w11_mg, "C24:1 (g)" => :c24_1_g, "C24:1w9 (mg)" => :c24_1w9_mg, "C24:1w11 (mg)" => :c24_1w11_mg, "C24:1w13 (mg)" => :c24_1w13_mg, "Total monounsaturated fatty acids, equated \n(g)" => :total_monounsaturated_fatty_acids_equated_g, "C12:2 (g)" => :c12_2_g, "C16:2w4 (mg)" => :c16_2w4_mg, "C16:3 (g)" => :c16_3_g, "C18:2w6 (g)" => :c18_2w6_g, "C18:3w3 (g)" => :c18_3w3_g, "C18:3w4 (g)" => :c18_3w4_g, "C18:3w6 (mg)" => :c18_3w6_mg, "C18:4w1 (g)" => :c18_4w1_g, "C18:4w3 (mg)" => :c18_4w3_mg, "C20:2 (mg)" => :c20_2_mg, "C20:2w6 (mg)" => :c20_2w6_mg, "C20:3 (mg)" => :c20_3_mg, "C20:3w3 (mg)" => :c20_3w3_mg, "C20:3w6 (mg)" => :c20_3w6_mg, "C20:4 (g)" => :c20_4_g, "C20:4w3 (mg)" => :c20_4w3_mg, "C20:4w6 (mg)" => :c20_4w6_mg, "C20:5w3 (mg)" => :c20_5w3_mg, "C21:5w3 (g)" => :c21_5w3_g, "C22:5w3 (mg)" => :c22_5w3_mg, "C22:4w6 (mg)" => :c22_4w6_mg, "C22:2 (g)" => :c22_2_g, "C22:2w6 (mg)" => :c22_2w6_mg, "C22:5w6 (g)" => :c22_5w6_g, "C22:6w3 (mg)" => :c22_6w3_mg, "Total polyunsaturated fatty acids, equated \n(g)" => :total_polyunsaturated_fatty_acids_equated_g, "Total long chain omega 3 fatty acids, equated \n(mg)" => :total_long_chain_omega_3_fatty_acids_equated_mg, "Total undifferentiated fatty acids, mass basis basis \n(mg)" => :total_undifferentiated_fatty_acids_mass_basis_basis_mg, "Total trans fatty acids, imputed \n(mg)" => :total_trans_fatty_acids_imputed_mg, "Caffeine \n(mg)" => :caffeine_mg, "Cholesterol \n(mg)" => :cholesterol_mg, "Alanine \n(mg/gN)" => :alanine_mg_gn, "Arginine \n(mg/gN)" => :arginine_mg_gn, "Aspartic acid \n(mg/gN)" => :aspartic_acid_mg_gn, "Cystine plus cysteine \n(mg/gN)" => :cystine_plus_cysteine_mg_gn, "Glutamic acid \n(mg/gN)" => :glutamic_acid_mg_gn, "Glycine \n(mg/gN)" => :glycine_mg_gn, "Histidine \n(mg/gN)" => :histidine_mg_gn, "Isoleucine \n(mg/gN)" => :isoleucine_mg_gn, "Leucine \n(mg/gN)" => :leucine_mg_gn, "Lysine \n(mg/gN)" => :lysine_mg_gn, "Methionine \n(mg/gN)" => :methionine_mg_gn, "Phenylalanine \n(mg/gN)" => :phenylalanine_mg_gn, "Proline \n(mg/gN)" => :proline_mg_gn, "Serine \n(mg/gN)" => :serine_mg_gn, "Threonine \n(mg/gN)" => :threonine_mg_gn, "Tyrosine \n(mg/gN)" => :tyrosine_mg_gn, "Tryptophan \n(mg/gN)" => :tryptophan_mg_gn, "Valine \n(mg/gN)" => :valine_mg_gn, "Alanine \n(mg)" => :alanine_mg, "Arginine \n(mg)" => :arginine_mg, "Aspartic acid \n(mg)" => :aspartic_acid_mg, "Cystine plus cysteine \n(mg)" => :cystine_plus_cysteine_mg, "Glutamic acid \n(mg)" => :glutamic_acid_mg, "Glycine \n(mg)" => :glycine_mg, "Histidine \n(mg)" => :histidine_mg, "Isoleucine \n(mg)" => :isoleucine_mg, "Leucine \n(mg)" => :leucine_mg, "Lysine \n(mg)" => :lysine_mg, "Methionine \n(mg)" => :methionine_mg, "Phenylalanine \n(mg)" => :phenylalanine_mg, "Proline \n(mg)" => :proline_mg, "Serine \n(mg)" => :serine_mg, "Threonine \n(mg)" => :threonine_mg, "Tyrosine \n(mg)" => :tyrosine_mg, "Tryptophan \n(mg)" => :tryptophan_mg, "Valine \n(mg)" => :valine_mg }.freeze csv_file = File.read(Rails.root.join('lib', 'tasks', 'australian_food_classification_data.csv')) csv = CSV.parse(csv_file, headers: true) csv.each do |row| record = AustralianFoodClassificationData.find_or_initialize_by( public_food_key: row.fetch("Public Food Key") ) attributes = {} HEADER_MAP.each do |csv_header, db_column| raw_value = row[csv_header] value = if raw_value&.match?(/\A-?[\d,.]+\z/) raw_value.gsub(',', '') else raw_value end attributes[db_column] = value end record.assign_attributes(attributes) record.save! end end end