Class: Alimento::Plato

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, &block) ⇒ Plato

Returns a new instance of Plato.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/alimento/plato.rb', line 6

def initialize(name, &block)
  @name = name
  @vegetales = []
  @frutas = []
  @integrales = []
  @proteinas = []
  @aceites = []
  @bebidas = []

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

Instance Attribute Details

#aceitesObject

Returns the value of attribute aceites.



4
5
6
# File 'lib/alimento/plato.rb', line 4

def aceites
  @aceites
end

#bebidasObject

Returns the value of attribute bebidas.



4
5
6
# File 'lib/alimento/plato.rb', line 4

def bebidas
  @bebidas
end

#frutasObject

Returns the value of attribute frutas.



4
5
6
# File 'lib/alimento/plato.rb', line 4

def frutas
  @frutas
end

#integralesObject

Returns the value of attribute integrales.



4
5
6
# File 'lib/alimento/plato.rb', line 4

def integrales
  @integrales
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/alimento/plato.rb', line 4

def name
  @name
end

#proteinasObject

Returns the value of attribute proteinas.



4
5
6
# File 'lib/alimento/plato.rb', line 4

def proteinas
  @proteinas
end

#vegetalesObject

Returns the value of attribute vegetales.



4
5
6
# File 'lib/alimento/plato.rb', line 4

def vegetales
  @vegetales
end

Instance Method Details

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



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

def aceite( nombre, opcion = {})
  ac = procesar(nombre, opcion)
  aceites << ac
end

#bebida(nombre, opcion = {}) ⇒ Object



49
50
51
52
# File 'lib/alimento/plato.rb', line 49

def bebida( nombre, opcion = {})
  beb = procesar(nombre, opcion)
  bebidas << beb
end

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



34
35
36
37
# File 'lib/alimento/plato.rb', line 34

def cereal( nombre, opcion = {})
  int = procesar(nombre, opcion)
  integrales << int
end

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



29
30
31
32
# File 'lib/alimento/plato.rb', line 29

def fruta( nombre, opcion = {})
  frut = procesar(nombre, opcion)
  frutas << frut
end

#procesar(nombre, opcion) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/alimento/plato.rb', line 54

def procesar(nombre, opcion)
  if opcion[:porcion]
    c_array = opcion[:porcion].split(' ')
    mult_arr = c_array[0].split('/')
    if mult_arr.length == 2
      mult = mult_arr[0].to_f / mult_arr[1].to_f
    else
      mult = mult_arr[0].to_f
    end
    h_search = ""
    c_array.each_with_index {|el, i| if i > 0; h_search = h_search + el + "( |s )"; end }
    s = @@porcion.keys.grep %r[#{h_search}]
    prop = @@porcion[s[0]]
    val_en_std = 0;
    al = @@tablaAlimento.find { |alimento| alimento.nombre == nombre }
    val_en_std = al.calcValEn if al
    val_en_total = mult * prop * (val_en_std.to_f / 100.0)
    elemento = [nombre, val_en_total]
  elsif opcion[:gramos]
    val_en_std = 0;
    al = @@tablaAlimento.find { |alimento| alimento.nombre == nombre }
    val_en_std = al.calcValEn if al
    val_total = (opcion[:gramos] * val_en_std).to_f / 100
    elemento = [nombre, val_total]
  end
end

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



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

def proteina( nombre, opcion = {})
  prot = procesar(nombre, opcion)
  proteinas << prot
end

#to_sObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/alimento/plato.rb', line 81

def to_s
  output = @name
  output << "\n#{'=' * @name.size}"
  veg  = frut = cer = prot = ac = beb = 0
  @vegetales.each  {|x| veg  += x[1]}
  @frutas.each     {|x| frut += x[1]}
  @integrales.each {|x| cer  += x[1]}
  @proteinas.each  {|x| prot += x[1]}
  @aceites.each    {|x| ac   += x[1]}
  @bebidas.each    {|x| beb  += x[1]}

  total = veg + frut + cer + prot + ac + beb
  output << "\n\nVegetales:\t#{veg.round(2)}\t#{(100*veg/total).round(2)}%\n\n  "
  @vegetales.each { |x| output << @@tablaAlimento.find{ |i| x[0] == i.nombre}.to_s; output << "\t#{x[1].round(2)}\n  "}

  output << "\n\nFrutas:\t#{frut.round(2)}\t#{(100*frut/total).round(2)}%\n\n  "
  @frutas.each { |x| output << @@tablaAlimento.find{ |i| x[0] == i.nombre}.to_s; output << "\t#{x[1].round(2)}\n  "}

  output << "\n\nCereales:\t#{cer.round(2)}\t#{(100*cer/total).round(2)}%\n\n  "
  @integrales.each { |x| output << @@tablaAlimento.find{ |i| x[0] == i.nombre}.to_s; output << "\t#{x[1].round(2)}\n  "}

  output << "\n\nProteinas:\t#{prot.round(2)}\t#{(100*prot/total).round(2)}%\n\n  "
  @proteinas.each { |x| output << @@tablaAlimento.find{ |i| x[0] == i.nombre}.to_s; output << "\t#{x[1].round(2)}\n  "}

  output << "\n\nAceites:\t#{ac.round(2)}\t#{(100*ac/total).round(2)}%\n\n  "
  @aceites.each { |x| output << @@tablaAlimento.find{ |i| x[0] == i.nombre}.to_s; output << "\t#{x[1].round(2)}\n  "}

  output << "\n\nBebidas:\t#{beb.round(2)}\t#{(100*beb/total).round(2)}%\n\n  "
  @bebidas.each { |x| output << @@tablaAlimento.find{ |i| x[0] == i.nombre}.to_s; output << "\t#{x[1].round(2)}\n  "}

  output << "\n\nTotal: #{total.round(2)}"
  output
end

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



24
25
26
27
# File 'lib/alimento/plato.rb', line 24

def vegetal( nombre, opcion = {})
  veg = procesar(nombre, opcion)
  vegetales << veg
end