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



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

#gramsObject

Returns the value of attribute grams.



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

def grams
  @grams
end

#ingredientsObject

Returns the value of attribute ingredients.



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

def ingredients
  @ingredients
end

#nameObject

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, options = {})
  alimento = $alimentos.find{ |x| x.nombre == name }
  cantidad = amount(options)
  @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(options = {})
  cantidad = 1
  cantidad = options[:gramos] if options[:gramos]
  cantidad = options[:porcion].chars.reject{ |x| x =~ /[^\d.\/]/ }.join("").to_r*15 if /cucharada/ =~ options[:porcion]
  cantidad = options[:porcion].chars.reject{ |x| x =~ /[^\d.\/]/ }.join("").to_r*250 if /cucharón/ =~ options[:porcion]
  cantidad = options[:porcion].chars.reject{ |x| x =~ /[^\d.\/]/ }.join("").to_r*225 if /taza/ =~ options[:porcion]
  cantidad = options[:porcion].chars.reject{ |x| x =~ /[^\d.\/]/ }.join("").to_r*220 if /pieza/ =~ options[:porcion]
  cantidad = options[:porcion].chars.reject{ |x| x =~ /[^\d.\/]/ }.join("").to_r*110 if /pieza/ =~ options[:porcion] && /pequeña/ =~ options[:porcion]
  cantidad
end

#cereal(name, options = {}) ⇒ Object



86
87
88
89
90
91
# File 'lib/alimento/plato.rb', line 86

def cereal(name, options = {})
  alimento = $alimentos.find{ |x| x.nombre == name }
  cantidad = amount(options)
  @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, options = {})
  alimento = $alimentos.find{ |x| x.nombre == name }
  cantidad = amount(options)
  @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, options = {})
  alimento = $alimentos.find{ |x| x.nombre == name }
  cantidad = amount(options)
  @ingredients << alimento
  @grams << cantidad
end

#to_sObject



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, options = {})
  alimento = $alimentos.find{ |x| x.nombre == name }
  cantidad = amount(options)
  @ingredients << alimento
  @grams << cantidad
end