class Alimento::Plato

Attributes

cantidad[RW]
ingredientes[RW]
name[RW]

Public Class Methods

new(name) { |self| ... } click to toggle source
# 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

Public Instance Methods

aceite(name, options = {}) click to toggle source
# File lib/alimento/plato.rb, line 67
def aceite(name, options = {})
   ingrediente(name, options)
end
bebida(name, options = {}) click to toggle source
# File lib/alimento/plato.rb, line 71
def bebida(name, options = {})
   ingrediente(name, options)
end
calorias_totales() click to toggle source
# 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 = {}) click to toggle source
# File lib/alimento/plato.rb, line 59
def cereal(name, options = {})
    ingrediente(name, options)
end
fruta(name, options = {}) click to toggle source
# File lib/alimento/plato.rb, line 55
def fruta(name, options = {})
    ingrediente(name, options)
end
ingrediente(name, options = {}) click to toggle source
# 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 = {}) click to toggle source
# File lib/alimento/plato.rb, line 63
def proteina(name, options = {})
    ingrediente(name, options)
end
to_s() click to toggle source
# 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 = {}) click to toggle source
# File lib/alimento/plato.rb, line 51
def vegetal(name, options = {})
    ingrediente(name, options)
end