Class: Alimento::Plato

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, &block) ⇒ Plato

Returns a new instance of Plato.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/alimento/plato.rb', line 34

def initialize(name, &block)

    @name = name
    @ingredientes = []
    @cantidad = []
    
    if block_given?
    
        if block.arity == 1
            yield self
        else
            instance_eval(&block) 
        end
  
    end
end

Instance Attribute Details

#cantidadObject

Returns the value of attribute cantidad.



32
33
34
# File 'lib/alimento/plato.rb', line 32

def cantidad
  @cantidad
end

#ingredientesObject

Returns the value of attribute ingredientes.



32
33
34
# File 'lib/alimento/plato.rb', line 32

def ingredientes
  @ingredientes
end

#nameObject

Returns the value of attribute name.



32
33
34
# File 'lib/alimento/plato.rb', line 32

def name
  @name
end

Instance Method Details

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



67
68
69
# File 'lib/alimento/plato.rb', line 67

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

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



71
72
73
# File 'lib/alimento/plato.rb', line 71

def bebida(name, options = {})
   ingrediente(name, options)
end

#calorias_totalesObject



96
97
98
99
100
101
102
103
104
105
# File 'lib/alimento/plato.rb', line 96

def calorias_totales
    
    calorias = 0
    
    (0..(@ingredientes.size-1)).each do |i|
        calorias += @ingredientes[i].valor_energetico.to_f * (@cantidad[i].to_f / 100)    
    end
    
    return calorias
end

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



59
60
61
# File 'lib/alimento/plato.rb', line 59

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

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



55
56
57
# File 'lib/alimento/plato.rb', line 55

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

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



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/alimento/plato.rb', line 75

def ingrediente(name, options = {})
  
    alimento = $alimentos.find { |i| i.nombre == name}
    
    gramos = 0
    
    if options.has_key?(:porcion)
        valor     = options[:porcion].partition(" ").first
        cantidad  = options[:porcion]
        cantidad.slice! (valor + " ")
        
        gramos = valor.to_r * $equivalencias[cantidad]
    else
        gramos = options[:gramos]
    end
    
    @ingredientes << alimento
    @cantidad     << gramos
    
end

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



63
64
65
# File 'lib/alimento/plato.rb', line 63

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

#to_sObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/alimento/plato.rb', line 107

def to_s
    output = @name
    output << "\n"
    
    format = '%-18s %-10s %-10s %-10s %-15s'
    format << "\n"
    output << format % [' ', 'Glúcidos', 'Proteínas', 'Lípidos', 'Valor energético']
    
    (0..(@ingredientes.size-1)).each do |i|
        
        nombre      = @ingredientes[i].nombre
        glucidos    = @ingredientes[i].glucidos.to_f           * (@cantidad[i].to_f / 100)
        proteinas   = @ingredientes[i].proteinas.to_f          * (@cantidad[i].to_f / 100) 
        lipidos     = @ingredientes[i].lipidos.to_f            * (@cantidad[i].to_f / 100) 
        v_energetico= @ingredientes[i].valor_energetico.to_f   * (@cantidad[i].to_f / 100) 
        
        output << format % [nombre, glucidos.round(2), proteinas.round(2), lipidos.round(2), v_energetico.round(2)]
    end
    
    total_calorias = calorias_totales()
    output << format % ['Total Calorias',' ', ' ',' ', total_calorias.round(2)]
    
    return output
end

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



51
52
53
# File 'lib/alimento/plato.rb', line 51

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