Class: FoodAbstract
- Inherits:
-
Object
- Object
- FoodAbstract
- Defined in:
- lib/food/food_class.rb
Overview
Abstract class for Food
Direct Known Subclasses
Instance Attribute Summary collapse
- #energetic_content ⇒ Object readonly
- #glucid_quantity ⇒ Object readonly
- #lipid_quantity ⇒ Object readonly
- #name ⇒ Object readonly
- #protein_quantity ⇒ Object readonly
Instance Method Summary collapse
-
#calculate_energetic_content ⇒ double
Calculates the energetic content for the food.
-
#initialize(name, protein_energy_pair, glucid_energy_pair, lipid_energy_pair) ⇒ FoodAbstract
constructor
Constructor of Abstract Food for allowing the childs to call it.
-
#to_s ⇒ String
Return string with the output for the food.
Constructor Details
#initialize(name, protein_energy_pair, glucid_energy_pair, lipid_energy_pair) ⇒ FoodAbstract
Constructor of Abstract Food for allowing the childs to call it.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/food/food_class.rb', line 23 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 @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 end |
Instance Attribute Details
#energetic_content ⇒ Object (readonly)
16 17 18 |
# File 'lib/food/food_class.rb', line 16 def energetic_content @energetic_content end |
#glucid_quantity ⇒ Object (readonly)
16 17 18 |
# File 'lib/food/food_class.rb', line 16 def glucid_quantity @glucid_quantity end |
#lipid_quantity ⇒ Object (readonly)
16 17 18 |
# File 'lib/food/food_class.rb', line 16 def lipid_quantity @lipid_quantity end |
#name ⇒ Object (readonly)
16 17 18 |
# File 'lib/food/food_class.rb', line 16 def name @name end |
#protein_quantity ⇒ Object (readonly)
16 17 18 |
# File 'lib/food/food_class.rb', line 16 def protein_quantity @protein_quantity end |
Instance Method Details
#calculate_energetic_content ⇒ double
Calculates the energetic content for the food
48 49 50 51 52 |
# File 'lib/food/food_class.rb', line 48 def calculate_energetic_content energetic_content = 0 @pair_macronutrient_energy.each{ |macronutrient, energy| energetic_content += (macronutrient * energy) } return energetic_content end |
#to_s ⇒ String
Return string with the output for the food
56 57 58 59 |
# File 'lib/food/food_class.rb', line 56 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 |