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
- #glucemic_ ⇒ Object readonly
- #glucid_quantity ⇒ Object readonly
- #lipid_quantity ⇒ Object readonly
- #name ⇒ Object readonly
- #protein_quantity ⇒ Object readonly
Instance Method Summary collapse
- #*(quantity) ⇒ Object
-
#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.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# 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 |
Instance Attribute Details
#energetic_content ⇒ Object (readonly)
17 18 19 |
# File 'lib/food/food_class.rb', line 17 def energetic_content @energetic_content end |
#glucemic_ ⇒ Object (readonly)
17 18 19 |
# File 'lib/food/food_class.rb', line 17 def glucemic_ @glucemic_ end |
#glucid_quantity ⇒ Object (readonly)
17 18 19 |
# File 'lib/food/food_class.rb', line 17 def glucid_quantity @glucid_quantity end |
#lipid_quantity ⇒ Object (readonly)
17 18 19 |
# File 'lib/food/food_class.rb', line 17 def lipid_quantity @lipid_quantity end |
#name ⇒ Object (readonly)
17 18 19 |
# File 'lib/food/food_class.rb', line 17 def name @name end |
#protein_quantity ⇒ Object (readonly)
17 18 19 |
# File 'lib/food/food_class.rb', line 17 def protein_quantity @protein_quantity end |
Instance Method Details
#*(quantity) ⇒ Object
63 64 65 66 |
# File 'lib/food/food_class.rb', line 63 def *(quantity) @energetic_content = (@energetic_content * quantity).round(3) return self end |
#calculate_energetic_content ⇒ double
Calculates the energetic content for the food
50 51 52 53 54 |
# 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 ⇒ String
Return string with the output for the food
58 59 60 61 |
# 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 |