Class: DSL

Inherits:
Object
  • Object
show all
Defined in:
lib/P06/DSL.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(nombre, alimentos, &block) ⇒ DSL

Returns a new instance of DSL.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/P06/DSL.rb', line 5

def initialize(nombre, alimentos, &block)
  @nombre = nombre
  @datos = []
  @alimentos = alimentos
  @total = 0
  
  if block_given?  
    if block.arity == 1
      yield self
    else
      instance_eval(&block) 
    end
  end
end

Instance Attribute Details

#datosObject

Returns the value of attribute datos.



3
4
5
# File 'lib/P06/DSL.rb', line 3

def datos
  @datos
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/P06/DSL.rb', line 3

def name
  @name
end

#totalObject

Returns the value of attribute total.



3
4
5
# File 'lib/P06/DSL.rb', line 3

def total
  @total
end

Instance Method Details

#aceite(nombre, opciones = {}) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/P06/DSL.rb', line 105

def aceite(nombre, opciones = {})
  
  gramos = conversor(opciones)
  calorias = 0
  
  for i in (0..@alimentos.size-1)
    
    if @alimentos[i].nombre == nombre
      calorias = @alimentos[i].calorias*gramos
    end
    
  end
  
  @datos << calorias
end

#cereal(nombre, opciones = {}) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/P06/DSL.rb', line 73

def cereal(nombre, opciones = {})
  
  gramos = conversor(opciones)
  calorias = 0
  
  for i in (0..@alimentos.size-1)
    
    if @alimentos[i].nombre == nombre
      calorias = @alimentos[i].calorias*gramos
    end
    
  end
  
  @datos << calorias
end

#conversor(opciones) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/P06/DSL.rb', line 121

def conversor(opciones)
  gramos=0.0
  
  if opciones[:amount] =~ /(?i)pieza[s]?\s{0,4}/
    gramos=gramos+1.18
    opciones[:amount] =opciones[:amount].gsub(/(?i)pieza[s]?\s{0,4}/,"")
  end
  
  if opciones[:amount] =~ /(?i)taza[s]?\s{0,4}/
    gramos=gramos+2.974
    opciones[:amount] =opciones[:amount].gsub(/(?i)taza[s]?\s{0,4}/,"")
  end
  
  if opciones[:amount] =~ /(?i)cucharada[s]?\s{0,4}/
    gramos=gramos+0.089
    opciones[:amount] =opciones[:amount].gsub(/(?i)cucharada[s]?\s{0,4}/,"")
  end
  
  if opciones[:amount] =~ /(?i)cucharon(es)?\s{0,4}/
    gramos=gramos+9.778
    opciones[:amount] =opciones[:amount].gsub(/(?i)cucharon(es)?\s{0,4}/,"")
  end
  
  if opciones[:amount] =~ /(?i)pequeña[s]?\s{0,1}/
    gramos=gramos-0.2
    opciones[:amount] = opciones[:amount].gsub(/(?i)pequeña[s]?\s{0,1}/,"")
  end
  
  if opciones[:amount] =~ /\d\/\d/
    valor1 = /\d\//.match(opciones[:amount]).to_s.gsub(/\//,"")
    valor2 = /\/\d/.match(opciones[:amount]).to_s.gsub(/\//,"")
    resultado = valor1.to_f/valor2.to_f
    gramos=gramos*resultado
  end
  
  if opciones[:amount] =~ /\d/
    if gramos > 0
      gramos=gramos*opciones[:amount].to_f
      opciones[:amount] = opciones[:amount].gsub(/\d/,"")
    end
  else
    gramos=gramos+(opciones[:amount]/10)
  end
  
  gramos
  
end

#fruta(nombre, opciones = {}) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/P06/DSL.rb', line 57

def fruta(nombre, opciones = {})
  
  gramos = conversor(opciones)
  calorias = 0
  
  for i in (0..@alimentos.size-1)
    
    if @alimentos[i].nombre == nombre
      calorias = @alimentos[i].calorias*gramos
    end
    
  end
  
  @datos << calorias
end

#proteina(nombre, opciones = {}) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/P06/DSL.rb', line 89

def proteina(nombre, opciones = {})
  
  gramos = conversor(opciones)
  calorias = 0
  
  for i in (0..@alimentos.size-1)
    
    if @alimentos[i].nombre == nombre
      calorias = @alimentos[i].calorias*gramos
    end
    
  end
  
  @datos << calorias
end

#to_sObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/P06/DSL.rb', line 20

def to_s
  
  output = "\n#{@nombre}"
  output << "\n#{'=' * 80}\n\n"
  output << "Composicion nutricional:\n"
  output << "\t\t|Proteínas\tGlúcidos\tLípidos\t\tValor Energético\n"
  for i in (0..@alimentos.size-1)
    if @alimentos[i].nombre.size > 7
      output << "#{@alimentos[i].nombre}\t|#{@alimentos[i].proteinas}\t\t#{@alimentos[i].glucidos}\t\t#{@alimentos[i].grasas}\t\t#{@datos[i].round(2)}"
    else
      output << "#{@alimentos[i].nombre}\t\t|#{@alimentos[i].proteinas}\t\t#{@alimentos[i].glucidos}\t\t#{@alimentos[i].grasas}\t\t#{@datos[i].round(2)}"
    end
    @total = @total + datos[i]
    output << "\n"
  end
  output << "#{'=' * 80}\n\n"
  output << "Valor energético total\t\t\t\t\t\t#{@total.round(2)}\n\n"
  
  output
end

#vegetal(nombre, opciones = {}) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/P06/DSL.rb', line 41

def vegetal(nombre, opciones = {})
  
  gramos = conversor(opciones)
  calorias = 0
  
  for i in (0..@alimentos.size-1)
    
    if @alimentos[i].nombre == nombre
      calorias = @alimentos[i].calorias*gramos
    end
    
  end
  
  @datos << calorias
end