class Object

Constants

GLUCID_ENERGY

Constants of energy @param PROTEIN_ENERGY [int] protein energy. @param GLUCID_ENERGY [int] glucid energy. @param LIPID_ENERGY [int] lipid energy

INGREDIENT_DATABASE_FILENAME

CLASS FOR HARVARDISHES

INGREDIENT_DATABASE_GRAMS
LIPID_ENERGY
MUG_QUANTITY
PIECE_QUANTITY

INGREDIENT WEIGHT 10G

PROTEIN_ENERGY
SMALL_MUG_QUANTITY
SMALL_PIECE_QUANTITY
SMALL_SPOON_QUANTITY
SPOON_QUANTITY

Public Instance Methods

read_data(data_filename, samples_data_filename = "") click to toggle source

Method to read data by file @params data_filename [String] filename of the data @return [Array] Return array of food

# 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) click to toggle source
# 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) click to toggle source

USEFUL FUNCTIONS

# 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) click to toggle source
# 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