Class: Alimento::Plato
- Inherits:
-
Object
- Object
- Alimento::Plato
- Defined in:
- lib/alimento/plato.rb
Instance Attribute Summary collapse
-
#grams ⇒ Object
Returns the value of attribute grams.
-
#ingredients ⇒ Object
Returns the value of attribute ingredients.
-
#name ⇒ Object
Returns the value of attribute name.
Instance Method Summary collapse
- #aceite(name, options = {}) ⇒ Object
- #amount(options = {}) ⇒ Object
- #cereal(name, options = {}) ⇒ Object
- #fruta(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.
37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/alimento/plato.rb', line 37 def initialize(name, &block) @name = name @ingredients = [] @grams = [] if block_given? if block.arity == 1 yield self else instance_eval(&block) end end end |
Instance Attribute Details
#grams ⇒ Object
Returns the value of attribute grams.
35 36 37 |
# File 'lib/alimento/plato.rb', line 35 def grams @grams end |
#ingredients ⇒ Object
Returns the value of attribute ingredients.
35 36 37 |
# File 'lib/alimento/plato.rb', line 35 def ingredients @ingredients end |
#name ⇒ Object
Returns the value of attribute name.
35 36 37 |
# File 'lib/alimento/plato.rb', line 35 def name @name end |
Instance Method Details
#aceite(name, options = {}) ⇒ Object
100 101 102 103 104 105 |
# File 'lib/alimento/plato.rb', line 100 def aceite(name, = {}) alimento = $alimentos.find{ |x| x.nombre == name } cantidad = amount() @ingredients << alimento @grams << cantidad end |
#amount(options = {}) ⇒ Object
107 108 109 110 111 112 113 114 115 116 |
# File 'lib/alimento/plato.rb', line 107 def amount( = {}) cantidad = 1 cantidad = [:gramos] if [:gramos] cantidad = [:porcion].chars.reject{ |x| x =~ /[^\d.\/]/ }.join("").to_r*15 if /cucharada/ =~ [:porcion] cantidad = [:porcion].chars.reject{ |x| x =~ /[^\d.\/]/ }.join("").to_r*250 if /cucharón/ =~ [:porcion] cantidad = [:porcion].chars.reject{ |x| x =~ /[^\d.\/]/ }.join("").to_r*225 if /taza/ =~ [:porcion] cantidad = [:porcion].chars.reject{ |x| x =~ /[^\d.\/]/ }.join("").to_r*220 if /pieza/ =~ [:porcion] cantidad = [:porcion].chars.reject{ |x| x =~ /[^\d.\/]/ }.join("").to_r*110 if /pieza/ =~ [:porcion] && /pequeña/ =~ [:porcion] cantidad end |
#cereal(name, options = {}) ⇒ Object
86 87 88 89 90 91 |
# File 'lib/alimento/plato.rb', line 86 def cereal(name, = {}) alimento = $alimentos.find{ |x| x.nombre == name } cantidad = amount() @ingredients << alimento @grams << cantidad end |
#fruta(name, options = {}) ⇒ Object
79 80 81 82 83 84 |
# File 'lib/alimento/plato.rb', line 79 def fruta(name, = {}) alimento = $alimentos.find{ |x| x.nombre == name } cantidad = amount() @ingredients << alimento @grams << cantidad end |
#proteina(name, options = {}) ⇒ Object
93 94 95 96 97 98 |
# File 'lib/alimento/plato.rb', line 93 def proteina(name, = {}) alimento = $alimentos.find{ |x| x.nombre == name } cantidad = amount() @ingredients << alimento @grams << cantidad end |
#to_s ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/alimento/plato.rb', line 51 def to_s output = @name output << "\n#{'=' * @name.size}\n" output << "Composición nutricional:\n" output << "#{' ' * 16}glúcidos proteínas lípidos\tvalor energético\n" valor = 0.0 @ingredients.each_with_index do |ing, i| output << "#{ing}" output << "#{' ' * (16 - ing.nombre.size)}" output << "#{(ing.glucidos * @grams[i]).round(2)}\t " output << "#{(ing.proteinas * @grams[i]).round(2)}\t " output << "#{(ing.lipidos * @grams[i]).round(2)} \t" output << "#{(ing.kcal * @grams[i]).round(2)}\n" valor += ing.kcal * @grams[i] end output << "\nvalor energético total\t\t\t\t#{valor.round(2)}" output end |
#vegetal(name, options = {}) ⇒ Object
72 73 74 75 76 77 |
# File 'lib/alimento/plato.rb', line 72 def vegetal(name, = {}) alimento = $alimentos.find{ |x| x.nombre == name } cantidad = amount() @ingredients << alimento @grams << cantidad end |