class Plato

Constants

ACEITE
ALIMENTOS
ARROZ
CANTIDAD
HUEVO
LENTEJAS
PLATANO
TOMATE

Attributes

aceites[RW]
cereales[RW]
frutas[RW]
name[RW]
proteinas[RW]
vegetales[RW]

Public Class Methods

new(name) { |self| ... } click to toggle source
# File lib/Alimento/Plato.rb, line 6
def initialize(name, &block)
  @name = name
  @vegetales = []
  @frutas = []
  @cereales = []
  @proteinas = []
  @aceites = []
  @kcal_totales = 0

  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 128
def aceite(name, options = {})
  aceite = []
  aceite[0] = name
  aceite[1] = "#{options[:porcion]}" if options[:porcion]
  @aceites << aceite
end
cereal(name, options = {}) click to toggle source
# File lib/Alimento/Plato.rb, line 114
def cereal(name, options = {})
  cereal = []
  cereal[0] = name
  cereal[1] = "#{options[:porcion]}" if options[:porcion]
  @cereales << cereal
end
fruta(name, options = {}) click to toggle source
# File lib/Alimento/Plato.rb, line 107
def fruta(name, options = {})
  fruta = []
  fruta[0] = name
  fruta[1] = "#{options[:porcion]}" if options[:porcion]
  @frutas << fruta
end
line(vector) click to toggle source
# File lib/Alimento/Plato.rb, line 35
def line (vector)
  salida = ""
  vector.each{
    |value|
    index = ALIMENTOS.find_index{ |obj| obj.name == value[0] }
    salida << "\n#{ALIMENTOS[index].name}".ljust(16)
    salida << " #{ALIMENTOS[index].carbohydrates}".ljust(15)
    salida << " #{ALIMENTOS[index].proteins}".ljust(15)
    salida << " #{ALIMENTOS[index].lipids}".ljust(15)     
  
    n_o_p = /[0-9]+/.match(value[1])[0].to_i   #number_of_pieces
    t_o_p = /\D+/.match(value[1]).to_s         #type_of_piece

    if(t_o_p == '/')
      n_o_p = /[0-9]+\/[0-9]+/.match(value[1]).to_a
      dividendo = n_o_p[0][0].to_f

      puts dividendo
      divisor = n_o_p[0][2].to_i
      puts divisor
      n_o_p = dividendo / divisor 

      t_o_p = /\D+/.match(value[1].tr('/','')).to_s   
    end

    total_value = CANTIDAD[t_o_p] * n_o_p * ALIMENTOS[index].val_energ
    @kcal_totales += total_value
    salida << " %0.2f".ljust(15) % [total_value]
  }
  return salida
end
proteina(name, options = {}) click to toggle source
# File lib/Alimento/Plato.rb, line 121
def proteina(name, options = {})
  proteina = []
  proteina[0] = name
  proteina[1] = "#{options[:porcion]}" if options[:porcion]
  @proteinas << proteina
end
to_s() click to toggle source
# File lib/Alimento/Plato.rb, line 68
def to_s
  salida = @name
  salida << "\n#{'=' * @name.size}\n\n"
  salida << "Composición nutricional \n"    
  salida << "Nombre".ljust(15)
  salida << "Glucidos".ljust(15)
  salida << "Proteinas".ljust(15)
  salida << "Lípidos".ljust(15)
  salida << "Val.Energ.".ljust(15)

  aux = line(@vegetales)
  salida << "#{aux}"

  aux = line(@frutas)
  salida << "#{aux}"

  aux = line(@cereales)
  salida << "#{aux}"

  aux = line(@proteinas)
  salida << "#{aux}"

  aux = line(@aceites)
  salida << "#{aux}"
 

  salida << "\n\n ".ljust(47)
  salida << "kcal totales: %0.2f" % [@kcal_totales]
  salida
end
vegetal(name, options = {}) click to toggle source
# File lib/Alimento/Plato.rb, line 100
def vegetal(name, options = {})
  vegetal = []
  vegetal[0] = name
  vegetal[1] = "#{options[:porcion]}" if options[:porcion]
  @vegetales << vegetal
end