Class: Alimento::Alimento

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

Overview

Esta clase permite representar un alimento y calcular su valor energético.

Direct Known Subclasses

GrupoAlimento

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(nombre, proteinas, glucidos, lipidos, data) ⇒ Alimento

Se asignan el nombre y los valores gramos de proteínas, glúcidos y lípidos al alimento.



19
20
21
22
23
24
25
# File 'lib/alimento/alimento.rb', line 19

def initialize(nombre, proteinas, glucidos, lipidos, data)
    @nombre = nombre
    @proteinas = proteinas
    @glucidos = glucidos
    @lipidos = lipidos
    @data = data
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



16
17
18
# File 'lib/alimento/alimento.rb', line 16

def data
  @data
end

#glucidosObject (readonly)

Returns the value of attribute glucidos.



16
17
18
# File 'lib/alimento/alimento.rb', line 16

def glucidos
  @glucidos
end

#lipidosObject (readonly)

Returns the value of attribute lipidos.



16
17
18
# File 'lib/alimento/alimento.rb', line 16

def lipidos
  @lipidos
end

#nombreObject (readonly)

Returns the value of attribute nombre.



16
17
18
# File 'lib/alimento/alimento.rb', line 16

def nombre
  @nombre
end

#proteinasObject (readonly)

Returns the value of attribute proteinas.



16
17
18
# File 'lib/alimento/alimento.rb', line 16

def proteinas
  @proteinas
end

Instance Method Details

#<=>(other) ⇒ Object

Se define para incluir el mixin comparable. Se toma como valor para la comparación el nombre del alimento.



39
40
41
42
# File 'lib/alimento/alimento.rb', line 39

def <=>(other)
  return nil unless other.kind_of?Alimento
  valor_energetico <=> other.valor_energetico
end

#aibc(g) ⇒ Object

Calcula el área incremental bajo la curva del del vector de datos del alimento y de la glucosa. Necesario para calcular el IG del alimento



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/alimento/alimento.rb', line 47

def aibc (g)
    r = g.map do |i| 
        i.each_with_index.map do |j, it| 
            if it > 0 
              0.0 if (i[it] < i[0])
              (((i[it] - i[0]) + (i[it-1] - i[0])) / 2)*5 if (i[it] >= i[0])
              
                
            end
        end
    end
    
    s = r.map{ |i| i.reject{ |gij| gij.class == nil::NilClass}}
    s.map{ |i| i.reduce(0, :+) }
    
end

#igObject

Devuelve el índice glucémico del alimento para los datos que se le pasaron al objeto cuando fue instanciado.



72
73
74
# File 'lib/alimento/alimento.rb', line 72

def ig
  (data.map{ |i| ig_ind(i)}.reduce(:+))/data.size
end

#ig_ind(g) ⇒ Object

Calcula el indicie glucémico asociado a un único individuo. Necesario para calcular el IG del alimento



66
67
68
# File 'lib/alimento/alimento.rb', line 66

def ig_ind(g)
    (aibc(g)).reduce(:/) * 100
end

#to_sObject

Permite visualizar el alimento formateado.



28
29
30
# File 'lib/alimento/alimento.rb', line 28

def to_s
    return %Q(#{@nombre.capitalize}: #{@proteinas}gr de proteínas, #{@glucidos}gr de hidratos de carbono y #{@lipidos}g de grasas)
end

#valor_energeticoObject

Calcula el valor energético del alimento a partir de los gramos de proteínas, glúcidos y lípidos.



33
34
35
# File 'lib/alimento/alimento.rb', line 33

def valor_energetico 
    (@proteinas * 4 + @glucidos * 4 + @lipidos * 9)
end