Class: FoodAbstract

Inherits:
Object
  • Object
show all
Defined in:
lib/food/food_class.rb

Overview

Abstract class for Food

Direct Known Subclasses

Food

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • name (String)

    the name for the food.

  • protein_energy_pair (pair)

    pair of protein number and energy.

  • glucid_energy_pair (pair)

    pair of glucid number and energy

  • lipid_energy_pair (pair)

    pair of lipid number and energy

  • lipid_energy_pair (pair)

    pair of lipid number and energy



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_contentObject (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_quantityObject (readonly)



17
18
19
# File 'lib/food/food_class.rb', line 17

def glucid_quantity
  @glucid_quantity
end

#lipid_quantityObject (readonly)



17
18
19
# File 'lib/food/food_class.rb', line 17

def lipid_quantity
  @lipid_quantity
end

#nameObject (readonly)



17
18
19
# File 'lib/food/food_class.rb', line 17

def name
  @name
end

#protein_quantityObject (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_contentdouble

Calculates the energetic content for the food

Returns:

  • (double)

    energetic_content



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_sString

Return string with the output for the food

Returns:

  • (String)

    outpout of 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