Class: Alimento::Plato

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

Overview

Esta clase permite representar un plato

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, &block) ⇒ Plato

Se guarda el nombre del plato y se recibe un bloque con los ingredientes



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/plato/fuente.rb', line 10

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

  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.



7
8
9
# File 'lib/plato/fuente.rb', line 7

def aceites
  @aceites
end

#frutasObject

Returns the value of attribute frutas.



7
8
9
# File 'lib/plato/fuente.rb', line 7

def frutas
  @frutas
end

#granosObject

Returns the value of attribute granos.



7
8
9
# File 'lib/plato/fuente.rb', line 7

def granos
  @granos
end

#nameObject

Returns the value of attribute name.



7
8
9
# File 'lib/plato/fuente.rb', line 7

def name
  @name
end

#proteinasObject

Returns the value of attribute proteinas.



7
8
9
# File 'lib/plato/fuente.rb', line 7

def proteinas
  @proteinas
end

#vegetalesObject

Returns the value of attribute vegetales.



7
8
9
# File 'lib/plato/fuente.rb', line 7

def vegetales
  @vegetales
end

Instance Method Details

#ingrediente(grupo, nombre, medida) ⇒ Object

Guarda un alimento dentro de la clase



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/plato/fuente.rb', line 28

def ingrediente(grupo, nombre, medida)
  alimento = $alimentos.find{|x| x.nombre == nombre}
  aux = medida.split(" ").first.split("/")
  valor = 0
  if(aux.size > 1)
    valor = aux[0].to_f/aux[1].to_f
  else
    valor = aux[0].to_f
  end
  
  cantidad = $equivalencias[medida.sub("ñ", "n").split(" ").drop(1).each{|x| x.gsub!(/s\z/, "")}.reduce(:+).to_sym].to_f
  cantidad *= valor
 

  if(grupo == "vegetal" || grupo == "vegetales")
    @vegetales << [alimento, cantidad]
  elsif(grupo == "fruta" || grupo == "frutas")
    @frutas << [alimento, cantidad]
  elsif(grupo == "grano" || grupo == "granos" || grupo == "cereal" || grupo == "cereales")
    @granos << [alimento, cantidad]
  elsif(grupo == "proteina" || grupo == "proteína" || grupo == "proteinas" || grupo == "proteínas")
    @proteinas << [alimento, cantidad]
  elsif(grupo == "aceite" || grupo == "aceites")
    @aceites << [alimento, cantidad]
  else
    "#{grupo} no es un grupo de alimento permitido"
  end
end

#to_sObject

Muestra el contenido del plato



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/plato/fuente.rb', line 58

def to_s
  total = 0
  output = @name
  output << "\n#{'=' * @name.size}\n\n"
  output << "Composición nutricional:\n"
  output << "%20s" % "" + "%-10s" % "glúcidos" + "%-10s" %  "proteínas" + "%-10s" % "lípidos" + "%-20s" % "valor energético\n"
  @vegetales.each{ |x, y| output << "%-20s" % x.nombre + "%-10s" % x.glucidos + "%-10s" % x.proteinas + "%-10s" % x.lipidos + "%-20f\n" % (x.val_energ/100*y); total += x.val_energ/100*y}
  @frutas.each{ |x, y| output << "%-20s" % x.nombre + "%-10s" % x.glucidos + "%-10s" % x.proteinas + "%-10s" % x.lipidos + "%-20f\n" % (x.val_energ/100*y); total += x.val_energ/100*y}
  @granos.each{ |x, y| output << "%-20s" % x.nombre + "%-10s" % x.glucidos + "%-10s" % x.proteinas + "%-10s" % x.lipidos + "%-20f\n" % (x.val_energ/100*y); total += x.val_energ/100*y}
  @proteinas.each{ |x, y| output << "%-20s" % x.nombre + "%-10s" % x.glucidos + "%-10s" % x.proteinas + "%-10s" % x.lipidos + "%-20f\n" % (x.val_energ/100*y); total += x.val_energ/100*y}
  @aceites.each{ |x, y| output << "%-20s" % x.nombre + "%-10s" % x.glucidos + "%-10s" % x.proteinas + "%-10s" % x.lipidos + "%-20f\n" % (x.val_energ/100*y); total += x.val_energ/100*y}
  output << "%-41s" % "Valor energético total" + "%20f" % total
  output
end