Class: PlatoNutricional
- Inherits:
-
Object
- Object
- PlatoNutricional
- Includes:
- Comparable
- Defined in:
- lib/huella/plato_nutricional.rb
Overview
Representa a un Plato con su nombre, formado por un conjunto de alimentos con sus
respectivas cantidades. Esta clase encapsula m
Direct Known Subclasses
Instance Attribute Summary collapse
-
#listaAlimentos ⇒ Object
readonly
Returns the value of attribute listaAlimentos.
-
#listaCantidades ⇒ Object
readonly
Returns the value of attribute listaCantidades.
-
#nombre ⇒ Object
readonly
Returns the value of attribute nombre.
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
Compara dos Platos según su valor energético.
-
#==(other) ⇒ Object
Dos Platos son iguales si todos sus atributos lo son.
-
#getPorcentajeCarbohidratosPlato ⇒ Object
Retorna el porcentaje que aporta los carbohidratos en el valor energético de un plato.
-
#getPorcentajeLipidosPlato ⇒ Object
Retorna el porcentaje que aporta los lípidos en el valor energético de un plato.
-
#getPorcentajeProteinasPlato ⇒ Object
Retorna el porcentaje que aporta las porteinas en el valor energético de un plato.
-
#getValorEnergeticoPlato ⇒ Object
Retorna el valor energético de un plato.
-
#initialize(nombre, listaAlimentos, listaCantidades) ⇒ PlatoNutricional
constructor
A new instance of PlatoNutricional.
-
#to_s ⇒ Object
Retorna el Plato formateado.
Constructor Details
#initialize(nombre, listaAlimentos, listaCantidades) ⇒ PlatoNutricional
Returns a new instance of PlatoNutricional.
13 14 15 16 17 18 19 |
# File 'lib/huella/plato_nutricional.rb', line 13 def initialize(nombre, listaAlimentos, listaCantidades) @nombre = nombre @listaAlimentos = listaAlimentos @listaCantidades = listaCantidades end |
Instance Attribute Details
#listaAlimentos ⇒ Object (readonly)
Returns the value of attribute listaAlimentos.
11 12 13 |
# File 'lib/huella/plato_nutricional.rb', line 11 def listaAlimentos @listaAlimentos end |
#listaCantidades ⇒ Object (readonly)
Returns the value of attribute listaCantidades.
11 12 13 |
# File 'lib/huella/plato_nutricional.rb', line 11 def listaCantidades @listaCantidades end |
#nombre ⇒ Object (readonly)
Returns the value of attribute nombre.
11 12 13 |
# File 'lib/huella/plato_nutricional.rb', line 11 def nombre @nombre end |
Instance Method Details
#<=>(other) ⇒ Object
Compara dos Platos según su valor energético
89 90 91 92 93 94 |
# File 'lib/huella/plato_nutricional.rb', line 89 def <=>(other) #-1 si left < right # 0 si left == right # 1 si left > right self.getValorEnergeticoPlato <=> other.getValorEnergeticoPlato end |
#==(other) ⇒ Object
Dos Platos son iguales si todos sus atributos lo son
97 98 99 100 |
# File 'lib/huella/plato_nutricional.rb', line 97 def ==(other) (@nombre == other.nombre) && (@listaAlimentos == listaAlimentos) && (@listaCantidades == listaCantidades) end |
#getPorcentajeCarbohidratosPlato ⇒ Object
Retorna el porcentaje que aporta los carbohidratos en el valor energético de un plato
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/huella/plato_nutricional.rb', line 51 def getPorcentajeCarbohidratosPlato carbohidratosTotales = 0 @listaAlimentos.each_with_index do |alimento, index| carbohidratosTotales += alimento.carbohidratos * 4.00 * @listaCantidades.at(index) end ( (carbohidratosTotales * 100.00) / self.getValorEnergeticoPlato ).round(2) end |
#getPorcentajeLipidosPlato ⇒ Object
Retorna el porcentaje que aporta los lípidos en el valor energético de un plato
64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/huella/plato_nutricional.rb', line 64 def getPorcentajeLipidosPlato lipidosTotales = 0 @listaAlimentos.each_with_index do |alimento, index| lipidosTotales += alimento.lipidos * 9.00 * @listaCantidades.at(index) end ( (lipidosTotales * 100.00) / self.getValorEnergeticoPlato ).round(2) end |
#getPorcentajeProteinasPlato ⇒ Object
Retorna el porcentaje que aporta las porteinas en el valor energético de un plato
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/huella/plato_nutricional.rb', line 38 def getPorcentajeProteinasPlato proteinasTotales = 0 @listaAlimentos.each_with_index do |alimento, index| proteinasTotales += alimento.proteinas * 4.00 * @listaCantidades.at(index) end ( (proteinasTotales * 100.00) / self.getValorEnergeticoPlato ).round(2) end |
#getValorEnergeticoPlato ⇒ Object
Retorna el valor energético de un plato
22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/huella/plato_nutricional.rb', line 22 def getValorEnergeticoPlato valorEnergeticoTotal = 0 alimentoWithCantidad = @listaAlimentos.zip(@listaCantidades) alimentosMultiplicados = alimentoWithCantidad.map do |alimento, cantidad| alimento * cantidad end valorEnergeticoTotal = alimentosMultiplicados.reduce(:+).getValorEnergetico valorEnergeticoTotal.round(2) end |
#to_s ⇒ Object
Retorna el Plato formateado
77 78 79 80 81 82 83 84 85 86 |
# File 'lib/huella/plato_nutricional.rb', line 77 def to_s salida = [] @listaAlimentos.each_with_index do |elemento, index| salida << "[" + elemento.nombre + ", " + @listaCantidades.at(index).to_s + "]" end "[" + @nombre + " [" + salida.join(" ") + "]]" end |