Class: Plato

Inherits:
Object
  • Object
show all
Defined in:
lib/prct06/receta.rb

Constant Summary collapse

@@unityConverter =
{
  "piezas pequeñas" => 50,
  "pieza pequeña" => 50,
  "taza" => 200,
  "tazas" => 200,
  "cucharón" => 70,
  "cucharónes" => 70,
  "pieza" => 50,
  "piezas" => 50,
  "cucharada" => 10,
  "cucharadas" => 10
}
@@tabla =
[
HuevosLacteosHelados.new("Huevo frito", 14.1, 0.0, 19.5),
HuevosLacteosHelados.new("Leche vaca", 3.3, 4.8, 3.2),
HuevosLacteosHelados.new("Yogurt", 3.8, 4.9, 3.8),
HuevosLacteosHelados.new("Huevo", 3.3, 4.8, 3.2),
CarnesDerivados.new("Cerdo", 21.5, 0.0, 6.3),
CarnesDerivados.new("Ternera", 21.1, 0.0, 3.1),
CarnesDerivados.new("Pollo", 20.6, 0.0, 5.6),
PescadosMariscos.new("Bacalao", 17.7, 0.0, 0.4),
PescadosMariscos.new("Atún", 21.5, 0.0, 15.5),
PescadosMariscos.new("Salmón", 19.9, 0.0, 13.6),
AlimentosGrasos.new("Aceite de oliva", 0.0, 0.2, 99.6),
AlimentosGrasos.new("Mantequilla", 0.7, 0.0, 83.2),
AlimentosGrasos.new("Chocolate", 5.3, 47.0, 30.0),
AlimentosCarbohidratos.new("Azúcar", 0.0, 99.8, 0.0),
AlimentosCarbohidratos.new("Arroz", 6.8, 77.7, 0.6),
AlimentosCarbohidratos.new("Lentejas", 23.5, 52.0, 1.4),
AlimentosCarbohidratos.new("Papas", 2.0, 15.4, 0.1),
VerdurasHortalizas.new("Tomate", 1.0, 3.5, 0.2),
VerdurasHortalizas.new("Cebolla", 1.3, 5.8, 0.3),
VerdurasHortalizas.new("Calabaza", 1.1, 4.8, 0.1),
Frutas.new("Manzana", 0.3, 12.4, 0.4),
Frutas.new("Plátano", 1.2, 21.4, 0.2),
Frutas.new("Pera", 0.5, 12.7, 0.3)
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, &block) ⇒ Plato

Returns a new instance of Plato.



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/prct06/receta.rb', line 45

def initialize(name, &block)
  @name = name
  @ingredients = []
  @valorEnergeticoTotal = 0.0

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

Instance Attribute Details

#ingredientsObject

Returns the value of attribute ingredients.



4
5
6
# File 'lib/prct06/receta.rb', line 4

def ingredients
  @ingredients
end

#instructionsObject

Returns the value of attribute instructions.



4
5
6
# File 'lib/prct06/receta.rb', line 4

def instructions
  @instructions
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/prct06/receta.rb', line 4

def name
  @name
end

Instance Method Details

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



113
114
115
# File 'lib/prct06/receta.rb', line 113

def aceite(name, options = {})
  ingredient(name, options)
end

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



105
106
107
# File 'lib/prct06/receta.rb', line 105

def cereal(name, options = {})
  ingredient(name, options)
end

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



101
102
103
# File 'lib/prct06/receta.rb', line 101

def fruta(name, options = {})
  ingredient(name, options)
end

#ingredient(name, options) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/prct06/receta.rb', line 81

def ingredient(name, options)
  ingredient = ""
  ingredient << name
  name.length < 8 ? ingredient << "\t" : ingredient << ""
  amount = translateAmount(options)
  @@tabla.each do |alimento|
    if name == alimento.name
      ingredient << "\t#{alimento.glucidos}\t\t#{alimento.proteinas}\t\t#{alimento.lipidos}\t\t"
      energy = amount * alimento.energy / 100
      ingredient << "#{energy.round(2)}"
      @valorEnergeticoTotal += energy
      @ingredients << ingredient
    end
  end
end

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



109
110
111
# File 'lib/prct06/receta.rb', line 109

def proteina(name, options = {})
  ingredient(name, options)
end

#to_sObject



59
60
61
62
63
64
65
66
# File 'lib/prct06/receta.rb', line 59

def to_s
  output = @name
  output << "\n#{'=' * @name.size}\n"
  output << "Composición nutricional:\n"
  output << "\t\tglúcidos \tproteínas \tlípidos \tvalor energético\n"
  output << "#{@ingredients.join("\n")}"
  output << "\nValor energético total\t\t\t\t\t\t#{@valorEnergeticoTotal.round(2)}"
end

#translateAmount(amount) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/prct06/receta.rb', line 68

def translateAmount(amount)
  if amount[:gramos] != nil
    amount[:gramos]
  elsif amount[:porcion] != nil
    text = amount[:porcion].partition(" ")
    number = text[0].to_f
    amount = @@unityConverter[text[2].strip.to_s]
    number * amount
  else
    nil
  end
end

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



97
98
99
# File 'lib/prct06/receta.rb', line 97

def vegetal(name, options = {})
  ingredient(name, options)
end