Class: Alimento

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/nutrientes.rb

Overview

This class allows to represent a food. It includes the foods Macronutrients and a way to calculate the energetic value. The mixin Comparable is included.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(nombre, proteinas, glucidos, grasas) ⇒ Alimento

Name and percentage of proteins, carbohydrates and fats are assigned



12
13
14
15
16
17
18
19
20
# File 'lib/nutrientes.rb', line 12

def initialize (nombre, proteinas, glucidos, grasas)
  @nombre = nombre
  @proteinas = proteinas
  @glucidos = glucidos
  @grasas = grasas
  @concentracionThis = []
  @concentracionGlucosa = []

end

Instance Attribute Details

#glucidosObject (readonly)

Returns the value of attribute glucidos.



9
10
11
# File 'lib/nutrientes.rb', line 9

def glucidos
  @glucidos
end

#grasasObject (readonly)

Returns the value of attribute grasas.



9
10
11
# File 'lib/nutrientes.rb', line 9

def grasas
  @grasas
end

#nombreObject (readonly)

Returns the value of attribute nombre.



9
10
11
# File 'lib/nutrientes.rb', line 9

def nombre
  @nombre
end

#proteinasObject (readonly)

Returns the value of attribute proteinas.



9
10
11
# File 'lib/nutrientes.rb', line 9

def proteinas
  @proteinas
end

Instance Method Details

#<=>(anOther) ⇒ Object

This method is defined because we included the mixin Comparable We take the energetic Value as our means to compare two foods



35
36
37
38
39
40
41
# File 'lib/nutrientes.rb', line 35

def <=> (anOther)
  if anOther.is_a?(Alimento) == false
    nil
  else
    self.valorEnergetico <=> anOther.valorEnergetico
  end
end

#addMeasurement(alimento, glucosa) ⇒ Object

Adds a pair of measurements to the data of this food



44
45
46
47
# File 'lib/nutrientes.rb', line 44

def addMeasurement (alimento, glucosa)
  @concentracionThis << alimento
  @concentracionGlucosa << glucosa
end

#indiceGlucemicoObject

calculates the glucemic index of this food based on its values that were added in addMeasurement



51
52
53
54
55
56
57
58
# File 'lib/nutrientes.rb', line 51

def indiceGlucemico
  # AIBC
  aibc = lambda {|list| list.drop(1).zip(list.first(list.count - 1)).map {|i| i[0] < list.first ? 0 : (((i[0] - list.first) + (i[1] - list.first))/2) * 5}.reduce(:+)}
  # IG ind
  igIndAll = @concentracionThis.zip(@concentracionGlucosa).map{|dataPair| [aibc.call(dataPair[0]), aibc.call(dataPair[1])]}.map{|aibcPair| (aibcPair[0] / aibcPair[1]) * 100}
  # IG
  igIndAll.reduce(:+)/igIndAll.count
end

#to_sObject

returns this food in a nicely formatted way



23
24
25
# File 'lib/nutrientes.rb', line 23

def to_s 
  "#{@nombre}:\nProteínas:\t\t #{@proteinas} gramos\nGlúcidos:\t\t #{@glucidos} gramos\nLípidos:\t\t #{@grasas} gramos\n\t\t\t por 100 gramos\nValor energetico:\t #{valorEnergetico().round(1)}"
end

#valorEnergetico(gramos = 100) ⇒ Object

calculates the energetic value of a food, generally of 100 grams of this food. This can be changed by the user, depending on her needs



29
30
31
# File 'lib/nutrientes.rb', line 29

def valorEnergetico (gramos = 100) 
  ((@proteinas * 4 + @glucidos * 4 + @grasas * 9) / 100) * gramos
end