Class: Plato

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

Overview

require_relative “list.rb” require_relative “alimento_.rb”

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, &block) ⇒ Plato

Returns a new instance of Plato.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/alimento/plato.rb', line 10

def initialize name, &block 
        @name = name 
        @vegetables = []
        @cereals = []
        @fruits = []
        @proteins = []
        @oils = []
        @@equivalencias = {"cucharada"=>15, "cucharadas"=>15, "piezas pequeñas"=>25, "taza"=>185, "cucharón"=>160, "pieza"=>65}
        @valor_energetico = 0
        
        @@list = List.new(nil,nil)
        @@list.insert([  Alimento_.new("Huevo frito",14.1,0.0,19.5),
                        Alimento_.new("Leche vaca",3.3,4.8,3.2),
                        Alimento_.new("Yogurt",3.8,4.9,3.8),
                        Alimento_.new("Cerdo",21.5,0.0,6.3),
                        Alimento_.new("Ternera",21.1,0.0,3.1),
                        Alimento_.new("Pollo",20.6,0.0,5.6),
                        Alimento_.new("Bacalao",17.7,0.0,0.4),
                        Alimento_.new("Atún",21.5,0.0,15.5),
                        Alimento_.new("Salmón",19.9,0.0,13.6),
                        Alimento_.new("Aceite de oliva",0.0,0.2,99.6),
                        Alimento_.new("Mantequilla",0.7,0.0,83.2),
                        Alimento_.new("Chocolate",5.3,47.0,30.0),
                        Alimento_.new("Azúcar",0.0,99.8,0.0),
                        Alimento_.new("Arroz",6.8,77.7,0.6),
                        Alimento_.new("Lentejas",23.5,52.0,1.4),
                        Alimento_.new("Papas",2.0,15.4,0.1),
                        Alimento_.new("Tomate",1.0,3.5,0.2),
                        Alimento_.new("Cebolla",1.3,5.8,0.3),
                        Alimento_.new("Calabaza",1.1,4.8,0.1),
                        Alimento_.new("Manzana",0.3,12.4,0.4),
                        Alimento_.new("Plátano",1.2,21.4,0.2),
                        Alimento_.new("Pera",0.5,12.7,0.3)
                    ])
       
        instance_eval(&block)
end

Instance Attribute Details

#cerealsObject (readonly)

Returns the value of attribute cereals.



8
9
10
# File 'lib/alimento/plato.rb', line 8

def cereals
  @cereals
end

#fruitsObject (readonly)

Returns the value of attribute fruits.



8
9
10
# File 'lib/alimento/plato.rb', line 8

def fruits
  @fruits
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/alimento/plato.rb', line 8

def name
  @name
end

#oilsObject (readonly)

Returns the value of attribute oils.



8
9
10
# File 'lib/alimento/plato.rb', line 8

def oils
  @oils
end

#proteinsObject (readonly)

Returns the value of attribute proteins.



8
9
10
# File 'lib/alimento/plato.rb', line 8

def proteins
  @proteins
end

#valor_energeticoObject (readonly)

Returns the value of attribute valor_energetico.



8
9
10
# File 'lib/alimento/plato.rb', line 8

def valor_energetico
  @valor_energetico
end

#vegetablesObject (readonly)

Returns the value of attribute vegetables.



8
9
10
# File 'lib/alimento/plato.rb', line 8

def vegetables
  @vegetables
end

Instance Method Details

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



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/alimento/plato.rb', line 67

def cereal n_alimento, options = {}
                cantidad = 0
    alimento = @@list.detect { |x| n_alimento == x.name }
    if options[:gramos]
        cantidad = options[:gramos]
    elsif options[:porcion]
        cantidad = options[:porcion].split[0].to_r
        
        cadena = ""
        options[:porcion].split[1..cadena.length-1].each do |x|
            cadena += x + " "
        end
        cadena = cadena.chomp ' '
        cantidad = cantidad * @@equivalencias[cadena]
    end 
    @valor_energetico += alimento.valor_energetico*cantidad
    @cereals.push([alimento, (alimento.valor_energetico*cantidad).round(2)])
end

#fruit(n_alimento, options = {}) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/alimento/plato.rb', line 105

def fruit n_alimento, options = {}
    cantidad = 0
    alimento = @@list.detect { |x| n_alimento == x.name }
    if options[:gramos]
        cantidad = options[:gramos]
    elsif options[:porcion]
        cantidad = options[:porcion].split[0].to_r
        
        cadena = ""
        options[:porcion].split[1..cadena.length-1].each do |x|
            cadena += x + " "
        end
        cadena = cadena.chomp ' '
        cantidad = cantidad * @@equivalencias[cadena]
    end 
    @valor_energetico += alimento.valor_energetico*cantidad
    @fruits.push([alimento, (alimento.valor_energetico*cantidad).round(2)])
end

#oil(n_alimento, options = {}) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/alimento/plato.rb', line 124

def oil n_alimento, options = {}
    cantidad = 0
    alimento = @@list.detect { |x| n_alimento == x.name }
    if options[:gramos]
        cantidad = options[:gramos]
    elsif options[:porcion]
        cantidad = options[:porcion].split[0].to_r
        
        cadena = ""
        options[:porcion].split[1..cadena.length-1].each do |x|
            cadena += x + " "
        end
        cadena = cadena.chomp ' '
        cantidad = cantidad * @@equivalencias[cadena]
    end 
    @valor_energetico += alimento.valor_energetico*cantidad
    @oils.push([alimento, (alimento.valor_energetico*cantidad).round(2)])
end

#protein(n_alimento, options = {}) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/alimento/plato.rb', line 86

def protein n_alimento, options = {}
    cantidad = 0
    alimento = @@list.detect { |x| n_alimento == x.name }
    if options[:gramos]
        cantidad = options[:gramos]
    elsif options[:porcion]
        cantidad = options[:porcion].split[0].to_r
        
        cadena = ""
        options[:porcion].split[1..cadena.length-1].each do |x|
            cadena += x + " "
        end
        cadena = cadena.chomp ' '
        cantidad = cantidad * @@equivalencias[cadena]
    end 
    @valor_energetico += alimento.valor_energetico*cantidad
    @proteins.push([alimento, (alimento.valor_energetico*cantidad).round(2)])
end

#to_sObject



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/alimento/plato.rb', line 144

def to_s
    cadena = @name + "\n"
    cadena += "================================================================= \nComposición nutricional: \n"
    for i in 0..@vegetables.length-1 do
        cadena += "%s\t%s\n" % [@vegetables[i][0].to_s, @vegetables[i][1].to_s]
    end
    for i in 0..@cereals.length-1 do
        cadena += "%s\t%s\n" % [@cereals[i][0].to_s, @cereals[i][1].to_s]
    end
    for i in 0..@fruits.length-1 do
        cadena += "%s\t%s\n" % [@fruits[i][0].to_s, @fruits[i][1].to_s]
    end
    for i in 0..@proteins.length-1 do
        cadena += "%s\t%s\n" % [@proteins[i][0].to_s, @proteins[i][1].to_s]
    end
    for i in 0..@oils.length-1 do
        cadena += "%s\t%s\n" % [@oils[i][0].to_s, @oils[i][1].to_s]
    end
    cadena += "Valor energético:\t" + @valor_energetico.to_s + "\n"
    cadena
end

#vegetable(n_alimento, options = {}) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/alimento/plato.rb', line 48

def vegetable n_alimento, options = {}
    cantidad = 0
    alimento = @@list.detect { |x| n_alimento == x.name }
    if options[:gramos]
        cantidad = options[:gramos]
    elsif options[:porcion]
        cantidad = options[:porcion].split[0].to_r
        
        cadena = ""
        options[:porcion].split[1..cadena.length-1].each do |x|
            cadena += x + " "
        end
        cadena = cadena.chomp ' '
        cantidad = cantidad * @@equivalencias[cadena]
    end 
    @valor_energetico += alimento.valor_energetico*cantidad
    @vegetables.push([alimento, (alimento.valor_energetico*cantidad).round(2)])
end