Class: Gema6::Dieta

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/gema6/dieta.rb

Overview

Clase que representa una Dieta (menú dietético)

Direct Known Subclasses

GrupoAlimentos, GrupoEdad

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(titulo, porcentaje = nil, platos = [], vct = nil, proteinas = nil, grasas = nil, hidratos = nil, &block) ⇒ Dieta

Returns a new instance of Dieta.



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/gema6/dieta.rb', line 7

def initialize(titulo, porcentaje = nil, platos = [], vct = nil, proteinas = nil, grasas = nil, hidratos = nil, &block)
    @titulo = titulo
    @porcentaje = porcentaje
    @platos = platos
    @vct = vct
    @proteinas = proteinas
    @grasas = grasas
    @hidratos = hidratos
    
    if (block_given?)
        instance_eval &block
    end
end

Instance Attribute Details

#grasasObject (readonly)

Returns the value of attribute grasas.



6
7
8
# File 'lib/gema6/dieta.rb', line 6

def grasas
  @grasas
end

#hidratosObject (readonly)

Returns the value of attribute hidratos.



6
7
8
# File 'lib/gema6/dieta.rb', line 6

def hidratos
  @hidratos
end

#platosObject (readonly)

Returns the value of attribute platos.



6
7
8
# File 'lib/gema6/dieta.rb', line 6

def platos
  @platos
end

#porcentajeObject (readonly)

Returns the value of attribute porcentaje.



6
7
8
# File 'lib/gema6/dieta.rb', line 6

def porcentaje
  @porcentaje
end

#porcentaje_maxObject (readonly)

Returns the value of attribute porcentaje_max.



6
7
8
# File 'lib/gema6/dieta.rb', line 6

def porcentaje_max
  @porcentaje_max
end

#porcentaje_minObject (readonly)

Returns the value of attribute porcentaje_min.



6
7
8
# File 'lib/gema6/dieta.rb', line 6

def porcentaje_min
  @porcentaje_min
end

#proteinasObject (readonly)

Returns the value of attribute proteinas.



6
7
8
# File 'lib/gema6/dieta.rb', line 6

def proteinas
  @proteinas
end

#tituloObject (readonly)

Returns the value of attribute titulo.



6
7
8
# File 'lib/gema6/dieta.rb', line 6

def titulo
  @titulo
end

#vctObject (readonly)

Returns the value of attribute vct.



6
7
8
# File 'lib/gema6/dieta.rb', line 6

def vct
  @vct
end

Instance Method Details

#<=>(otro) ⇒ Object

Método utilizado para comparar un menú con otro.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/gema6/dieta.rb', line 63

def <=>(otro)
    #Cuando comparo un menú con otro, pruebo con el VCT, sino lo miramos segun la grasa, luego proteinas, y por ultimo los hidratos.
    if @vct < otro.vct
        -1
    elsif @vct > otro.vct
        1
    else
        # Si son iguales en VCT, mirar grasas...
        if @grasas < otro.grasas
            -1
        elsif @grasas > otro.grasas
            1
        else
        # Si son iguales en grasas, mirar en proteinas
            if @proteinas < otro.proteinas
                -1
            elsif @proteinas > otro.proteinas
                1
            else
                if @hidratos < otro.hidratos
                    -1
                elsif @hidratos > otro.hidratos
                    1
                else
                    # SON IGUALES
                    0
                end
            end
        end
    end
    
end

#getCjtoPlatos(i, k) ⇒ Object

Devuelve un array (conjunto) de platos especificado por parámetro.



32
33
34
35
36
37
38
# File 'lib/gema6/dieta.rb', line 32

def getCjtoPlatos(i, k)
    @cjto = []
    for p in i..k
        @cjto.push(@platos.at(p))
    end
    @cjto
end

#getDescripcion(i) ⇒ Object

Devuelve la descripción completa del plato indicado por parámetro



22
23
24
# File 'lib/gema6/dieta.rb', line 22

def getDescripcion(i)
    @platos.at(i).at(0)
end

#getGramos(i) ⇒ Object

Devuelve los gramos que contiene el plato indicado por parámetro.



46
47
48
# File 'lib/gema6/dieta.rb', line 46

def getGramos(i)
    getPlato(i).at(2)
end

#getPlato(i) ⇒ Object

Devuelve el plato indicado por parámetro



27
28
29
# File 'lib/gema6/dieta.rb', line 27

def getPlato(i)
    @platos.at(i)
end

#getPorcion(i) ⇒ Object

Devuelve la porción que conlleva el plato indicado por parámetro.



41
42
43
# File 'lib/gema6/dieta.rb', line 41

def getPorcion(i)
    getPlato(i).at(1)
end

#ingesta(options = {}) ⇒ Object



96
97
98
99
# File 'lib/gema6/dieta.rb', line 96

def ingesta(options = {})
    @porcentaje_min = options[:min]
    @porcentaje_max = options[:max]
end

#plato(options = {}) ⇒ Object



101
102
103
104
# File 'lib/gema6/dieta.rb', line 101

def plato(options = {})
    plato = [options[:descripcion], options[:porcion], options[:gramos]]
    @platos.push(plato)
end

#porcentajes(options = {}) ⇒ Object



106
107
108
109
110
111
# File 'lib/gema6/dieta.rb', line 106

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

#to_sObject

Devuelve una cadena que contiene la descripción completa del menú dietético.



51
52
53
54
55
56
57
58
59
60
# File 'lib/gema6/dieta.rb', line 51

def to_s
    @resultado = "#{@titulo} (#{@porcentaje}%)\n"
    for p in 0..(@platos.length - 1) 
        @resultado.concat("- #{getDescripcion(p)}, #{getPorcion(p)}, #{getGramos(p)} g \n")
    end
    @resultado.concat("V.C.T. | %\t#{@vct} kcal | #{@proteinas}% - #{@grasas}% - #{@hidratos}%")
    		
    		@resultado
    	
end