Class: Alimento::PlatoDSL

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

Overview

Plato formado por cantidades de uno o más alimentos.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, &block) ⇒ PlatoDSL

Returns a new instance of PlatoDSL.



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/alimento/plato_dsl.rb', line 9

def initialize(name, &block)
  @name = name
  @listaAlimentos = List.new()
  @listaGramos = List.new()

  if block_given?
    if block.arity == 1
      yield self
    else
      instance_eval(&block)
    end
  end
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/alimento/plato_dsl.rb', line 8

def name
  @name
end

Instance Method Details

#<=>(other) ⇒ Object



122
123
124
125
126
# File 'lib/alimento/plato_dsl.rb', line 122

def <=>(other)
  if other != nil
    getValorCaloricoTotal <=> other.getValorCaloricoTotal
  end
end

#alimento(alimento, options = {}) ⇒ Object



25
26
27
28
29
30
# File 'lib/alimento/plato_dsl.rb', line 25

def alimento(alimento, options = {})
  unless options[:gramos].nil?
    @listaAlimentos << alimento
    @listaGramos << options[:gramos]
  end
end

#getCarbohidratosObject

Devuelve el total de carbohidratos del plato.



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/alimento/plato_dsl.rb', line 60

def getCarbohidratos
  aux = @listaAlimentos.head
  aux2 = @listaGramos.head
  carbohidratos = 0
  gramosTotales = 0
  while aux != nil && @listaGramos != nil
    carbohidratos += aux.value.carbohidratos * (aux2.value/100)
    gramosTotales = aux2.value
    aux = aux.next
    aux2 = aux2.next
  end
  return ((carbohidratos * 100)/gramosTotales).round(2)
end

#getEmisionGEIObject

Devuelve las emisiones de gases de efecto invernadero de un plato.



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/alimento/plato_dsl.rb', line 91

def getEmisionGEI
  aux = @listaAlimentos.head
  aux2 = @listaGramos.head

  geiTotal = 0
  while aux != nil && aux2 != nil
    geiTotal += aux.value.GEI * (aux2.value / 100)
    aux = aux.next
    aux2 = aux2.next
  end
  geiTotal.round(2)
end

#getGramosTotalesObject

Devuelve la cantidad total de gramos del plato.



74
75
76
77
78
79
80
81
82
# File 'lib/alimento/plato_dsl.rb', line 74

def getGramosTotales
  aux = @listaGramos.head
  gramosTotales = 0
  while aux != nil
    gramosTotales = aux.value
    aux = aux.next
  end
  return gramosTotales
end

#getLipidosObject

Devuelve el total de lípidos del plato.



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/alimento/plato_dsl.rb', line 46

def getLipidos
  aux = @listaAlimentos.head
  aux2 = @listaGramos.head
  lipidos = 0
  gramosTotales = 0
  while aux != nil && @listaGramos != nil
    lipidos += aux.value.lipidos * (aux2.value/100)
    gramosTotales = aux2.value
    aux = aux.next
    aux2 = aux2.next
  end
  return ((lipidos * 100)/gramosTotales).round(2)
end

#getProteinasObject

Devuelve el total de proteínas del plato.



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/alimento/plato_dsl.rb', line 32

def getProteinas
  aux = @listaAlimentos.head
  aux2 = @listaGramos.head
  proteinas = 0
  gramosTotales = 0
  while aux != nil && @listaGramos != nil
    proteinas += aux.value.proteinas * (aux2.value/100)
    gramosTotales = aux2.value
    aux = aux.next
    aux2 = aux2.next
  end
  return ((proteinas * 100)/gramosTotales).round(2)
end

#getTerrenoObject

Devuelve el terreno consumido por los alimentos de un plato.



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/alimento/plato_dsl.rb', line 105

def getTerreno
  aux = @listaAlimentos.head
  aux2 = @listaGramos.head

  terrenoTotal = 0
  while aux != nil && aux2 != nil
    terrenoTotal = aux.value.terreno * (aux2.value / 100)
    aux = aux.next
  end
  terrenoTotal.round(2)
end

#getValorCaloricoTotalObject

Devuelve el total del valor calórico del plato.



84
85
86
87
88
89
# File 'lib/alimento/plato_dsl.rb', line 84

def getValorCaloricoTotal
  proteinas = (getProteinas * getGramosTotales) / 100
  carbohidratos = (getCarbohidratos * getGramosTotales) / 100
  lipidos = (getLipidos * getGramosTotales) / 100
  return (proteinas*4 + carbohidratos*9 + lipidos*4).round(2)
end

#nombre(name) ⇒ Object



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

def nombre(name)
  @name = name
end

#to_sObject



116
117
118
119
120
121
# File 'lib/alimento/plato_dsl.rb', line 116

def to_s
  ret = "#{@name}\n"
  ret + @listaAlimentos.zip(@listaGramos).map { 
    |x| x.first.to_s + " - " + x.last.to_s + " gramos"
  }.join("\n")
end