class DSL

Attributes

datos[RW]
name[RW]
total[RW]

Public Class Methods

new(nombre, alimentos) { |self| ... } click to toggle source
# File lib/P06/DSL.rb, line 5
def initialize(nombre, alimentos, &block)
        @nombre = nombre
        @datos = []
        @alimentos = alimentos
        @total = 0
        
        if block_given?  
                if block.arity == 1
                        yield self
                else
                        instance_eval(&block) 
                end
        end
end

Public Instance Methods

aceite(nombre, opciones = {}) click to toggle source
# File lib/P06/DSL.rb, line 105
def aceite(nombre, opciones = {})
        
        gramos = conversor(opciones)
        calorias = 0
        
        for i in (0..@alimentos.size-1)
                
                if @alimentos[i].nombre == nombre
                        calorias = @alimentos[i].calorias*gramos
                end
                
        end
        
        @datos << calorias
end
cereal(nombre, opciones = {}) click to toggle source
# File lib/P06/DSL.rb, line 73
def cereal(nombre, opciones = {})
        
        gramos = conversor(opciones)
        calorias = 0
        
        for i in (0..@alimentos.size-1)
                
                if @alimentos[i].nombre == nombre
                        calorias = @alimentos[i].calorias*gramos
                end
                
        end
        
        @datos << calorias
end
conversor(opciones) click to toggle source
# File lib/P06/DSL.rb, line 121
def conversor(opciones)
        gramos=0.0
        
        if opciones[:amount] =~ /(?i)pieza[s]?\s{0,4}/
                gramos=gramos+1.18
                opciones[:amount] =opciones[:amount].gsub(/(?i)pieza[s]?\s{0,4}/,"")
        end
        
        if opciones[:amount] =~ /(?i)taza[s]?\s{0,4}/
                gramos=gramos+2.974
                opciones[:amount] =opciones[:amount].gsub(/(?i)taza[s]?\s{0,4}/,"")
        end
        
        if opciones[:amount] =~ /(?i)cucharada[s]?\s{0,4}/
                gramos=gramos+0.089
                opciones[:amount] =opciones[:amount].gsub(/(?i)cucharada[s]?\s{0,4}/,"")
        end
        
        if opciones[:amount] =~ /(?i)cucharon(es)?\s{0,4}/
                gramos=gramos+9.778
                opciones[:amount] =opciones[:amount].gsub(/(?i)cucharon(es)?\s{0,4}/,"")
        end
        
        if opciones[:amount] =~ /(?i)pequeña[s]?\s{0,1}/
                gramos=gramos-0.2
                opciones[:amount] = opciones[:amount].gsub(/(?i)pequeña[s]?\s{0,1}/,"")
        end
        
        if opciones[:amount] =~ /\d\/\d/
                valor1 = /\d\//.match(opciones[:amount]).to_s.gsub(/\//,"")
                valor2 = /\/\d/.match(opciones[:amount]).to_s.gsub(/\//,"")
                resultado = valor1.to_f/valor2.to_f
                gramos=gramos*resultado
        end
        
        if opciones[:amount] =~ /\d/
                if gramos > 0
                        gramos=gramos*opciones[:amount].to_f
                        opciones[:amount] = opciones[:amount].gsub(/\d/,"")
                end
        else
                gramos=gramos+(opciones[:amount]/10)
        end
        
        gramos
        
end
fruta(nombre, opciones = {}) click to toggle source
# File lib/P06/DSL.rb, line 57
def fruta(nombre, opciones = {})
        
        gramos = conversor(opciones)
        calorias = 0
        
        for i in (0..@alimentos.size-1)
                
                if @alimentos[i].nombre == nombre
                        calorias = @alimentos[i].calorias*gramos
                end
                
        end
        
        @datos << calorias
end
proteina(nombre, opciones = {}) click to toggle source
# File lib/P06/DSL.rb, line 89
def proteina(nombre, opciones = {})
        
        gramos = conversor(opciones)
        calorias = 0
        
        for i in (0..@alimentos.size-1)
                
                if @alimentos[i].nombre == nombre
                        calorias = @alimentos[i].calorias*gramos
                end
                
        end
        
        @datos << calorias
end
to_s() click to toggle source
# File lib/P06/DSL.rb, line 20
def to_s
        
        output = "\n#{@nombre}"
        output << "\n#{'=' * 80}\n\n"
        output << "Composicion nutricional:\n"
        output << "\t\t|Proteínas\tGlúcidos\tLípidos\t\tValor Energético\n"
        for i in (0..@alimentos.size-1)
                if @alimentos[i].nombre.size > 7
                        output << "#{@alimentos[i].nombre}\t|#{@alimentos[i].proteinas}\t\t#{@alimentos[i].glucidos}\t\t#{@alimentos[i].grasas}\t\t#{@datos[i].round(2)}"
                else
                        output << "#{@alimentos[i].nombre}\t\t|#{@alimentos[i].proteinas}\t\t#{@alimentos[i].glucidos}\t\t#{@alimentos[i].grasas}\t\t#{@datos[i].round(2)}"
                end
                @total = @total + datos[i]
                output << "\n"
        end
        output << "#{'=' * 80}\n\n"
        output << "Valor energético total\t\t\t\t\t\t#{@total.round(2)}\n\n"
        
        output
end
vegetal(nombre, opciones = {}) click to toggle source
# File lib/P06/DSL.rb, line 41
def vegetal(nombre, opciones = {})
        
        gramos = conversor(opciones)
        calorias = 0
        
        for i in (0..@alimentos.size-1)
                
                if @alimentos[i].nombre == nombre
                        calorias = @alimentos[i].calorias*gramos
                end
                
        end
        
        @datos << calorias
end