class Menu

@author Daniel Darias Sánchez <alu0100783230@ull.edu.es>

Attributes

Content[R]
Etiqueta[R]
Porcentage[R]
Title[R]

Public Class Methods

new(title = "") { |self| ... } click to toggle source
# File lib/Prct07/Menu.rb, line 6
def initialize(title = "", &block)
  @Title = check_name title
  @Content = []

  if block_given?
    if block.arity == 1
      yield self
    else
      instance_eval &block
    end
  end
end

Public Instance Methods

<=>(other) click to toggle source

necessary for comparable module

# File lib/Prct07/Menu.rb, line 116
def <=>(other)
  get_vct <=> other.get_vct
end
check_content(content) click to toggle source

checks if the content is an array of plates (or derivated classes)

# File lib/Prct07/Menu.rb, line 54
def check_content(content)
  if (!content.kind_of?(Array))
    content = [Plate.new("Croqueta","1",20,Nutrition_Info.new(10, 15, 20, 25))]
  else
    content.map! do |x|
      if(!x.kind_of?(Plate))
        Plate.new("Croqueta","1",20,Nutrition_Info.new(10, 15, 20, 25))
      else
        x
      end
    end
  end
  content
end
check_name(title) click to toggle source

checks if the name match any of the allowed ones

# File lib/Prct07/Menu.rb, line 34
def check_name(title)
  posible_names = ["Desayuno", "Almuerzo", "Cena", "Media Mañana", "Merienda"]
  posible_names.each do |x|
    if (x == title)
      return title
    end
  end
  "Desayuno"
end
check_porcentage(porcentage) click to toggle source

checks if the porcentage is a valid number [0, 100]

# File lib/Prct07/Menu.rb, line 45
def check_porcentage(porcentage)
  if (porcentage > 0)
    porcentage
  else
    10
  end
end
etiqueta(etiq) click to toggle source
# File lib/Prct07/Menu.rb, line 19
def etiqueta etiq
  @Etiqueta = etiq
end
get_fats() click to toggle source

returns the total calories of all plates

# File lib/Prct07/Menu.rb, line 79
def get_fats
  var = 0
  @Content.each do |x|
    var +=  x.NutritionalInfo.Fats
  end
  var
end
get_hidrates() click to toggle source

returns the total hidrates of all plates

# File lib/Prct07/Menu.rb, line 88
def get_hidrates
  var = 0
  @Content.each do |x|
    var +=  x.NutritionalInfo.Hidrates
  end
  var
end
get_proteins() click to toggle source

returns the total proteins of all plates

# File lib/Prct07/Menu.rb, line 97
def get_proteins
  var = 0
  @Content.each do |x|
    var +=  x.NutritionalInfo.Proteins
  end
  var
end
get_vct() click to toggle source

returns the vct

# File lib/Prct07/Menu.rb, line 70
def get_vct
  var = 0
  @Content.each do |x|
    var +=  x.NutritionalInfo.Calories
  end
  var
end
plato(plato) click to toggle source
# File lib/Prct07/Menu.rb, line 27
def plato plato
  n = plato[:nutrition_info]
  nutritional = Nutrition_Info.new n[0], n[1], n[2], n[3]
  @Content << Plate.new(plato[:nombre], plato[:info_extra], plato[:cantidad], nutritional)
end
porcentage(pctg) click to toggle source
# File lib/Prct07/Menu.rb, line 23
def porcentage pctg
  @Porcentage = check_porcentage pctg
end
to_s() click to toggle source

necessary for puts method

# File lib/Prct07/Menu.rb, line 106
def to_s
  s = @Title + " (#{@Porcentage}%)\n"
  @Content.each do |x|
    s += "- " + x.to_s + "\n"

  end
  s += "V.C.T | % #{get_vct} kcal | #{get_hidrates}% | #{get_proteins}% | #{get_fats}%"
end