Class: Dieta

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/prct06/Dieta.rb

Overview

clase principal de la jerarquía

Direct Known Subclasses

DietaEdad, DietaGrupo

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(nombre, &block) ⇒ Dieta

constructor



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/prct06/Dieta.rb', line 13

def initialize (nombre, &block)
	self.nombre = nombre
	self.titulo
	self.porcentaje_ingesta
	self.nplatos

	self.platos = []

	self.vct
	self.proteinas
	self.grasas
	self.hidratos

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

Instance Attribute Details

#grasasObject

attr



10
11
12
# File 'lib/prct06/Dieta.rb', line 10

def grasas
  @grasas
end

#hidratosObject

attr



10
11
12
# File 'lib/prct06/Dieta.rb', line 10

def hidratos
  @hidratos
end

#nombreObject

attr



10
11
12
# File 'lib/prct06/Dieta.rb', line 10

def nombre
  @nombre
end

#nplatosObject

attr



10
11
12
# File 'lib/prct06/Dieta.rb', line 10

def nplatos
  @nplatos
end

#platosObject

attr



10
11
12
# File 'lib/prct06/Dieta.rb', line 10

def platos
  @platos
end

#porcentaje_ingestaObject

attr



10
11
12
# File 'lib/prct06/Dieta.rb', line 10

def porcentaje_ingesta
  @porcentaje_ingesta
end

#proteinasObject

attr



10
11
12
# File 'lib/prct06/Dieta.rb', line 10

def proteinas
  @proteinas
end

#tituloObject

attr



10
11
12
# File 'lib/prct06/Dieta.rb', line 10

def titulo
  @titulo
end

#vctObject

attr



10
11
12
# File 'lib/prct06/Dieta.rb', line 10

def vct
  @vct
end

Instance Method Details

#<=>(other) ⇒ Object

invalidación del <=> para hacer los objetos comparables



79
80
81
82
# File 'lib/prct06/Dieta.rb', line 79

def <=> (other)
	return nil unless other.is_a?Dieta
	self.vct <=> other.vct
end

#get_plato(n) ⇒ Object

acceso a un plato en concreto



71
72
73
74
75
76
# File 'lib/prct06/Dieta.rb', line 71

def get_plato (n)
	if n < 1 then
		return ""
	end
	return platos[n-1]
end

#ingesta(porcentaje = {}) ⇒ Object

función para definir el porcentaje de ingesta. Podría darse una estimación entre dos



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/prct06/Dieta.rb', line 41

def ingesta(porcentaje = {})
	standard = porcentaje[:fijo] if porcentaje[:fijo]
	low = porcentaje[:min] if porcentaje[:min]
	up = porcentaje[:max] if porcentaje[:max]

	if porcentaje[:fijo] then
		self.porcentaje_ingesta = "#{standard}%"
	else
		self.porcentaje_ingesta = "#{low}% - #{up}%"
	end
end

#plato(options = {}) ⇒ Object

función para definir platos nuevos



62
63
64
65
66
67
68
# File 'lib/prct06/Dieta.rb', line 62

def plato(options = {})
	nuevo = []
	nuevo << "#{options[:descripcion]}" if options[:descripcion]
	nuevo << "#{options[:porcion]}" if options[:porcion]
	nuevo << options[:gramos] if options[:gramos]
	platos << nuevo
end

#porcentajes(options = {}) ⇒ Object

función para definir los porcentajes de grasa, proteínas, hidratos y el valor calórico total



54
55
56
57
58
59
# File 'lib/prct06/Dieta.rb', line 54

def porcentajes(options = {})
	self.vct = options[:vct] if options[:vct]
	self.proteinas = options[:proteinas] if options[:proteinas]
	self.grasas = options[:grasas] if options[:grasas]
	self.hidratos = options[:hidratos] if options[:hidratos]
end

#tipo(ntitulo) ⇒ Object

función para definir el título del menú: almuerzo, cena, etc.



36
37
38
# File 'lib/prct06/Dieta.rb', line 36

def tipo(ntitulo)
	self.titulo = ntitulo
end

#to_sObject

formateo a string



85
86
87
88
89
90
91
92
93
94
# File 'lib/prct06/Dieta.rb', line 85

def to_s
	x = ""
	x << "#{titulo} (#{porcentaje_ingesta})\n"

	platos.each do |i|
		x << "- #{i[0]}, #{i[1]}, #{i[2]}g\n"
	end

	x << "V.C.T | %    #{vct} kcal | #{proteinas} #{grasas} #{hidratos}\n\n"
end