Class: Alimento::Plato
- Inherits:
-
Object
- Object
- Alimento::Plato
- Defined in:
- lib/alimento/plato.rb
Instance Attribute Summary collapse
-
#cantidad ⇒ Object
Returns the value of attribute cantidad.
-
#ingredientes ⇒ Object
Returns the value of attribute ingredientes.
-
#name ⇒ Object
Returns the value of attribute name.
Instance Method Summary collapse
- #aceite(name, options = {}) ⇒ Object
- #bebida(name, options = {}) ⇒ Object
- #calorias_totales ⇒ Object
- #cereal(name, options = {}) ⇒ Object
- #fruta(name, options = {}) ⇒ Object
- #ingrediente(name, options = {}) ⇒ Object
-
#initialize(name, &block) ⇒ Plato
constructor
A new instance of Plato.
- #proteina(name, options = {}) ⇒ Object
- #to_s ⇒ Object
- #vegetal(name, options = {}) ⇒ Object
Constructor Details
#initialize(name, &block) ⇒ Plato
Returns a new instance of Plato.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/alimento/plato.rb', line 34 def initialize(name, &block) @name = name @ingredientes = [] @cantidad = [] if block_given? if block.arity == 1 yield self else instance_eval(&block) end end end |
Instance Attribute Details
#cantidad ⇒ Object
Returns the value of attribute cantidad.
32 33 34 |
# File 'lib/alimento/plato.rb', line 32 def cantidad @cantidad end |
#ingredientes ⇒ Object
Returns the value of attribute ingredientes.
32 33 34 |
# File 'lib/alimento/plato.rb', line 32 def ingredientes @ingredientes end |
#name ⇒ Object
Returns the value of attribute name.
32 33 34 |
# File 'lib/alimento/plato.rb', line 32 def name @name end |
Instance Method Details
#aceite(name, options = {}) ⇒ Object
67 68 69 |
# File 'lib/alimento/plato.rb', line 67 def aceite(name, = {}) ingrediente(name, ) end |
#bebida(name, options = {}) ⇒ Object
71 72 73 |
# File 'lib/alimento/plato.rb', line 71 def bebida(name, = {}) ingrediente(name, ) end |
#calorias_totales ⇒ Object
96 97 98 99 100 101 102 103 104 105 |
# File 'lib/alimento/plato.rb', line 96 def calorias_totales calorias = 0 (0..(@ingredientes.size-1)).each do |i| calorias += @ingredientes[i].valor_energetico.to_f * (@cantidad[i].to_f / 100) end return calorias end |
#cereal(name, options = {}) ⇒ Object
59 60 61 |
# File 'lib/alimento/plato.rb', line 59 def cereal(name, = {}) ingrediente(name, ) end |
#fruta(name, options = {}) ⇒ Object
55 56 57 |
# File 'lib/alimento/plato.rb', line 55 def fruta(name, = {}) ingrediente(name, ) end |
#ingrediente(name, options = {}) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/alimento/plato.rb', line 75 def ingrediente(name, = {}) alimento = $alimentos.find { |i| i.nombre == name} gramos = 0 if .has_key?(:porcion) valor = [:porcion].partition(" ").first cantidad = [:porcion] cantidad.slice! (valor + " ") gramos = valor.to_r * $equivalencias[cantidad] else gramos = [:gramos] end @ingredientes << alimento @cantidad << gramos end |
#proteina(name, options = {}) ⇒ Object
63 64 65 |
# File 'lib/alimento/plato.rb', line 63 def proteina(name, = {}) ingrediente(name, ) end |
#to_s ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/alimento/plato.rb', line 107 def to_s output = @name output << "\n" format = '%-18s %-10s %-10s %-10s %-15s' format << "\n" output << format % [' ', 'Glúcidos', 'Proteínas', 'Lípidos', 'Valor energético'] (0..(@ingredientes.size-1)).each do |i| nombre = @ingredientes[i].nombre glucidos = @ingredientes[i].glucidos.to_f * (@cantidad[i].to_f / 100) proteinas = @ingredientes[i].proteinas.to_f * (@cantidad[i].to_f / 100) lipidos = @ingredientes[i].lipidos.to_f * (@cantidad[i].to_f / 100) v_energetico= @ingredientes[i].valor_energetico.to_f * (@cantidad[i].to_f / 100) output << format % [nombre, glucidos.round(2), proteinas.round(2), lipidos.round(2), v_energetico.round(2)] end total_calorias = calorias_totales() output << format % ['Total Calorias',' ', ' ',' ', total_calorias.round(2)] return output end |
#vegetal(name, options = {}) ⇒ Object
51 52 53 |
# File 'lib/alimento/plato.rb', line 51 def vegetal(name, = {}) ingrediente(name, ) end |