class FoodAbstract

Abstract class for Food

Attributes

energetic_content[R]

@attr_reader name [String] name the name of the food @attr_reader protein_quantity [pair] pair of protein number and energy @attr_reader glucid_quantity [pair] pair of glucid number and energy @attr_reader lipid_quantity [pair] pair of lipid number and energy @attr_reader energetic_content [double] energetic content of the food

glucemic_[R]

@attr_reader name [String] name the name of the food @attr_reader protein_quantity [pair] pair of protein number and energy @attr_reader glucid_quantity [pair] pair of glucid number and energy @attr_reader lipid_quantity [pair] pair of lipid number and energy @attr_reader energetic_content [double] energetic content of the food

glucid_quantity[R]

@attr_reader name [String] name the name of the food @attr_reader protein_quantity [pair] pair of protein number and energy @attr_reader glucid_quantity [pair] pair of glucid number and energy @attr_reader lipid_quantity [pair] pair of lipid number and energy @attr_reader energetic_content [double] energetic content of the food

lipid_quantity[R]

@attr_reader name [String] name the name of the food @attr_reader protein_quantity [pair] pair of protein number and energy @attr_reader glucid_quantity [pair] pair of glucid number and energy @attr_reader lipid_quantity [pair] pair of lipid number and energy @attr_reader energetic_content [double] energetic content of the food

name[R]

@attr_reader name [String] name the name of the food @attr_reader protein_quantity [pair] pair of protein number and energy @attr_reader glucid_quantity [pair] pair of glucid number and energy @attr_reader lipid_quantity [pair] pair of lipid number and energy @attr_reader energetic_content [double] energetic content of the food

protein_quantity[R]

@attr_reader name [String] name the name of the food @attr_reader protein_quantity [pair] pair of protein number and energy @attr_reader glucid_quantity [pair] pair of glucid number and energy @attr_reader lipid_quantity [pair] pair of lipid number and energy @attr_reader energetic_content [double] energetic content of the food

Public Class Methods

new(name, protein_energy_pair, glucid_energy_pair, lipid_energy_pair) click to toggle source

Constructor of Abstract Food for allowing the childs to call it. @param name [String] the name for the food. @param protein_energy_pair [pair] pair of protein number and energy. @param glucid_energy_pair [pair] pair of glucid number and energy @param lipid_energy_pair [pair] pair of lipid number and energy @param lipid_energy_pair [pair] pair of lipid number and energy

# File lib/food/food_class.rb, line 25
def initialize (name, protein_energy_pair, glucid_energy_pair, lipid_energy_pair)
  raise unless name.is_a? String
  raise unless ((protein_energy_pair.is_a? Array) && (glucid_energy_pair.is_a? Array) && (lipid_energy_pair.is_a? Array))
  raise unless ((protein_energy_pair.count == 2) && (glucid_energy_pair.count == 2) && (lipid_energy_pair.count == 2))

  protein_energy_pair.each { |element| raise unless element.is_a?(Integer) || element.is_a?(Float) }
  glucid_energy_pair.each { |element| raise unless element.is_a?(Integer) || element.is_a?(Float) }
  lipid_energy_pair.each { |element| raise unless element.is_a?(Integer) || element.is_a?(Float) }
  
  @name = name.capitalize
  @protein_quantity = protein_energy_pair[0]
  @glucid_quantity = glucid_energy_pair[0]
  @lipid_quantity = lipid_energy_pair[0]
  
  # Vector de pares, pues hash no permite iguales
  @pair_macronutrient_energy = []
  @pair_macronutrient_energy.push([protein_energy_pair[0], protein_energy_pair[1]])
  @pair_macronutrient_energy.push([glucid_energy_pair[0], glucid_energy_pair[1]])
  @pair_macronutrient_energy.push([lipid_energy_pair[0], lipid_energy_pair[1]])
  
  @energetic_content = calculate_energetic_content.round(3)
end

Public Instance Methods

*(quantity) click to toggle source
# File lib/food/food_class.rb, line 63
def *(quantity)
  @energetic_content = (@energetic_content * quantity).round(3)
  return self
end
calculate_energetic_content() click to toggle source

Calculates the energetic content for the food @return [double] energetic_content

# File lib/food/food_class.rb, line 50
def calculate_energetic_content
  energetic_content = 0
  @pair_macronutrient_energy.each{ |macronutrient, energy| energetic_content += (macronutrient * energy) }
  return energetic_content
end
to_s() click to toggle source

Return string with the output for the food @return [String] outpout of food

# File lib/food/food_class.rb, line 58
def to_s
  "Nombre: #{@name} | Proteínas: #{@protein_quantity} gramos | Glúcidos: #{@glucid_quantity} gramos | Lípidos: #{@lipid_quantity} gramos | " \
  "Contenido Energético: #{@energetic_content} Kcal."
end