Top Level Namespace
Defined Under Namespace
Modules: DLLModule, FoodGem Classes: Array, Food, FoodAbstract, HarvardDishDSL
Constant Summary collapse
- GLUCID_ENERGY =
Constants of energy
4
- LIPID_ENERGY =
9
- PROTEIN_ENERGY =
4
- INGREDIENT_DATABASE_FILENAME =
CLASS FOR HARVARDISHES
"input/food-data.txt"
- INGREDIENT_DATABASE_GRAMS =
100
- PIECE_QUANTITY =
INGREDIENT WEIGHT 10G
100
- SMALL_PIECE_QUANTITY =
PIECE_QUANTITY * 0.5
- MUG_QUANTITY =
240
- SMALL_MUG_QUANTITY =
MUG_QUANTITY * 0.5
- SPOON_QUANTITY =
150
- SMALL_SPOON_QUANTITY =
SPOON_QUANTITY * 0.5
Instance Method Summary collapse
-
#read_data(data_filename, samples_data_filename = "") ⇒ Array
Method to read data by file.
- #read_ingredient_database(data_filename) ⇒ Object
-
#read_recipe(recipe_filename) ⇒ Object
USEFUL FUNCTIONS.
- #read_samples_data(samples_data_filename) ⇒ Object
Instance Method Details
#read_data(data_filename, samples_data_filename = "") ⇒ Array
Method to read data by file
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/food/functions.rb', line 71 def read_data (data_filename, samples_data_filename = "") data_string = File.open(data_filename).read.split("\n") # Divido el fichero en string de lineas food_array = [] if (samples_data_filename != "") sample_people_hash = read_samples_data(samples_data_filename) end data_string.each { |data_line| data_line = data_line.split(" ") # La divido en espacios name = "" while (data_line[0] != data_line[0].to_f.to_s) # Si el nombre no cambia al pasar de string afloat es que es un float name << data_line[0] << " " data_line = data_line[1..-1] # Quito el primer elemento end food_name = name[0..-2].capitalize protein = [data_line[0].to_f, PROTEIN_ENERGY] glucid = [data_line[1].to_f, GLUCID_ENERGY] lipid = [data_line[2].to_f, LIPID_ENERGY] data_line = data_line[3..-1] group_name = "" while (!data_line[0].nil?) # Si el nombre no cambia al pasar de string afloat es que es un float group_name << data_line[0] << " " data_line = data_line[1..-1] # Quito el primer elemento end if (samples_data_filename != "") food = Food.new(food_name, protein, glucid, lipid, group_name[0..-2], sample_people_hash[food_name]) else food = Food.new(food_name, protein, glucid, lipid, group_name[0..-2]) end food_array.push(food) } return food_array end |
#read_ingredient_database(data_filename) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/food/functions.rb', line 114 def read_ingredient_database(data_filename) data_string = File.open(data_filename).read.split("\n") # Divido el fichero en string de lineas food_hash = Hash.new() data_string.each { |data_line| data_line = data_line.split(" ") # La divido en espacios name = "" while (data_line[0] != data_line[0].to_f.to_s) # Si el nombre no cambia al pasar de string afloat es que es un float name << data_line[0] << " " data_line = data_line[1..-1] # Quito el primer elemento end food_name = name[0..-2].capitalize protein = [data_line[0].to_f, PROTEIN_ENERGY] glucid = [data_line[1].to_f, GLUCID_ENERGY] lipid = [data_line[2].to_f, LIPID_ENERGY] data_line = data_line[3..-1] group_name = "" while (!data_line[0].nil?) group_name << data_line[0] << " " data_line = data_line[1..-1] # Quito el primer elemento end food_hash[food_name] = Food.new(food_name, protein, glucid, lipid, group_name[0..-2]) } return food_hash end |
#read_recipe(recipe_filename) ⇒ Object
USEFUL FUNCTIONS
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/food/functions.rb', line 3 def read_recipe (recipe_filename) recipes = File.open(recipe_filename).read.split("\n") # Divido el fichero en string de lineas # hash nombre_receta ingredientes(proc) recipe_ingredient_hash = Hash.new() # Por cada receta ( Que tenga un { ) recipes.grep(/{/).each { |recipe_title| # Coger el índice donde empieza y donde termina (Que tenga un }) start_index = recipes.find_index{ |line| line == recipe_title } end_index = recipes[start_index..-1].find_index{ |line| line == "}" } # Cogemos lo que está dentro de los dos índices (la receta) recipe_ingredient_hash[recipe_title[0..-3]] = recipes[start_index+1, end_index-1] # .join("\n") } return recipe_ingredient_hash end |
#read_samples_data(samples_data_filename) ⇒ Object
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 |
# File 'lib/food/functions.rb', line 24 def read_samples_data (samples_data_filename) data_file = File.open(samples_data_filename) data_file = data_file.read.split("\n")[1..-1] # Divido el fichero en líneas y quito la primera data_line_array = data_file.collect! { |data_array| data_array.split(" ") } # Cambio las líneas a arrays # Hash with (name of the food, sample array for all the persons to that foods) sample_people_hash = Hash.new([]) person_number = 1 line_counter = 0 # Mientras no hayamos recorrido todas las lineas y el primero sea un int (nuevo individuo) while ((line_counter < data_line_array.count) && (data_line_array[line_counter][0].to_i == person_number)) data_line = data_line_array[line_counter] person_number = person_number + 1 # Change of person data_line = data_line[1..-1] # Delete the person number while ((line_counter < data_line_array.count) && (data_line_array[line_counter][0].to_i != person_number)) food_name = data_line[0].capitalize sample_person_array = data_line[1..-1].collect { |data| data.to_f } # Cambio numeros a float if (sample_people_hash[food_name] == []) if (food_name == "Glucosa") sample_people_hash.each_key { |name| sample_people_hash[name][person_number-2].push(sample_person_array) } else sample_people_hash[food_name] = [[sample_person_array]] end else sample_people_hash[food_name].push([sample_person_array]) end line_counter = line_counter + 1 # Cambio de línea unless (line_counter >= data_line_array.count) data_line = data_line_array[line_counter] end end end #sample_people_hash.each{ |x, y| puts "#{x} => #{y}" } return sample_people_hash end |