Class: Alimento

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

Overview

Representa un alimento con su nombre y sus

* valores energéticos: Proteinas carbohidratos y lipidos
* valores ambientales: Gases de efecto invernadero (gei) y uso de terreno

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(nombre, proteinas, carbohidratos, lipidos, gei, terreno) ⇒ Alimento

Returns a new instance of Alimento.



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

def initialize(nombre, proteinas, carbohidratos, lipidos, gei, terreno)
    @nombre = nombre
    @proteinas = proteinas
    @carbohidratos = carbohidratos
    @lipidos = lipidos
    @gei = gei
    @terreno = terreno
end

Instance Attribute Details

#carbohidratosObject (readonly)

Returns the value of attribute carbohidratos.



10
11
12
# File 'lib/huella/alimento.rb', line 10

def carbohidratos
  @carbohidratos
end

#geiObject (readonly)

Returns the value of attribute gei.



10
11
12
# File 'lib/huella/alimento.rb', line 10

def gei
  @gei
end

#lipidosObject (readonly)

Returns the value of attribute lipidos.



10
11
12
# File 'lib/huella/alimento.rb', line 10

def lipidos
  @lipidos
end

#nombreObject (readonly)

Returns the value of attribute nombre.



10
11
12
# File 'lib/huella/alimento.rb', line 10

def nombre
  @nombre
end

#proteinasObject (readonly)

Returns the value of attribute proteinas.



10
11
12
# File 'lib/huella/alimento.rb', line 10

def proteinas
  @proteinas
end

#terrenoObject (readonly)

Returns the value of attribute terreno.



10
11
12
# File 'lib/huella/alimento.rb', line 10

def terreno
  @terreno
end

Instance Method Details

#*(value) ⇒ Object

Retorna un nuevo Alimento resultado de la multiplicación de un Alimento por un escalar



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/huella/alimento.rb', line 100

def *(value)

    proNew = (@proteinas * value).round(2)
    carboNew = (@carbohidratos * value).round(2)
    lipNew = (@lipidos * value).round(2)
    geiNew = (@gei * value).round(2)
    terNew = (@terreno * value).round(2)

    Alimento.new(@nombre, proNew, carboNew, lipNew, geiNew, terNew)

end

#+(other) ⇒ Object

Retorna un nuevo Alimento resultado de la suma de otros dos alimentos



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/huella/alimento.rb', line 87

def +(other)

    proNew = (other.proteinas + @proteinas).round(2)
    carboNew = (other.carbohidratos + @carbohidratos).round(2)
    lipNew = (other.lipidos + @lipidos).round(2)
    geiNew = (other.gei + @gei).round(2)
    terNew = (other.terreno + @terreno).round(2)

    Alimento.new("combinado", proNew, carboNew, lipNew, geiNew, terNew)

end

#<=>(other) ⇒ Object

Compara cada Alimento según su valor energético



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

def <=>(other)
    #-1 si left <  right
    # 0 si left == right
    # 1 si left >  right
    self.getValorEnergetico <=> other.getValorEnergetico
end

#==(other) ⇒ Object

Dos Alimentos son iguales si tienen los mismos atributos



45
46
47
48
49
# File 'lib/huella/alimento.rb', line 45

def ==(other)
    (@nombre == other.nombre) && (@proteinas == other.proteinas) && 
        (@carbohidratos == other.carbohidratos) && (@lipidos == other.lipidos) && 
        (@gei == other.gei) && (@terreno == other.terreno) 
end

#getGeix100grObject

Como el valor de gei está en 1 Kg, se hace la equivalencia a 100 gr



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

def getGeix100gr
    (@gei * 100).round(2)
end

#getIndiceEnergia(cantidad) ⇒ Object

Retorna un índice de energía según la cantidad en gr del alimento



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/huella/alimento.rb', line 52

def getIndiceEnergia(cantidad)

    energia = self.getValorEnergetico * cantidad

    if(energia < 670)
        return 1
    elsif(energia < 830)
        return 2
    else
        return 3
    end

end

#getIndiceHuellaCarbono(cantidad) ⇒ Object

Retorna un índice de huella de carbono según la cantidad en gr del alimento



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/huella/alimento.rb', line 72

def getIndiceHuellaCarbono(cantidad)

    gases = self.getGeix100gr * cantidad

    if(gases < 800)
        return 1
    elsif(gases < 1200)
        return 2
    else
        return 3
    end

end

#getValorEnergeticoObject

Define la proporcion de cada nutriente en el aporte energético



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

def getValorEnergetico
    ( (proteinas * 4.00) + (carbohidratos * 4.00) + (lipidos * 9.00) ).round(2)
end

#getValorEnergeticoConGramos(cantidad) ⇒ Object

Retorna el valor energético dependiendo de la cantidad de gramos del alimento



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

def getValorEnergeticoConGramos(cantidad)
    ( (getValorEnergetico * cantidad) / 100.0 ).round(2)
end

#to_sObject

Retorna el Alimento formateado



22
23
24
# File 'lib/huella/alimento.rb', line 22

def to_s
    "[#{@nombre} [#{@proteinas} #{@carbohidratos} #{@lipidos}] [#{@gei} #{@terreno}]]"
end