Class: Menu

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/Prct07/Menu.rb

Overview

Author:

Direct Known Subclasses

MenuAge, MenuContent

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title = "", &block) ⇒ Menu

Returns a new instance of Menu.



6
7
8
9
10
11
12
13
14
15
16
17
# 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

Instance Attribute Details

#ContentObject (readonly)

Returns the value of attribute Content.



4
5
6
# File 'lib/Prct07/Menu.rb', line 4

def Content
  @Content
end

#EtiquetaObject (readonly)

Returns the value of attribute Etiqueta.



4
5
6
# File 'lib/Prct07/Menu.rb', line 4

def Etiqueta
  @Etiqueta
end

#PorcentageObject (readonly)

Returns the value of attribute Porcentage.



4
5
6
# File 'lib/Prct07/Menu.rb', line 4

def Porcentage
  @Porcentage
end

#TitleObject (readonly)

Returns the value of attribute Title.



4
5
6
# File 'lib/Prct07/Menu.rb', line 4

def Title
  @Title
end

Instance Method Details

#<=>(other) ⇒ Object

necessary for comparable module



116
117
118
# File 'lib/Prct07/Menu.rb', line 116

def <=>(other)
  get_vct <=> other.get_vct
end

#check_content(content) ⇒ Object

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



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# 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) ⇒ Object

checks if the name match any of the allowed ones



34
35
36
37
38
39
40
41
42
# 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) ⇒ Object

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



45
46
47
48
49
50
51
# File 'lib/Prct07/Menu.rb', line 45

def check_porcentage(porcentage)
  if (porcentage > 0)
    porcentage
  else
    10
  end
end

#etiqueta(etiq) ⇒ Object



19
20
21
# File 'lib/Prct07/Menu.rb', line 19

def etiqueta etiq
  @Etiqueta = etiq
end

#get_fatsObject

returns the total calories of all plates



79
80
81
82
83
84
85
# File 'lib/Prct07/Menu.rb', line 79

def get_fats
  var = 0
  @Content.each do |x|
    var +=  x.NutritionalInfo.Fats
  end
  var
end

#get_hidratesObject

returns the total hidrates of all plates



88
89
90
91
92
93
94
# File 'lib/Prct07/Menu.rb', line 88

def get_hidrates
  var = 0
  @Content.each do |x|
    var +=  x.NutritionalInfo.Hidrates
  end
  var
end

#get_proteinsObject

returns the total proteins of all plates



97
98
99
100
101
102
103
# File 'lib/Prct07/Menu.rb', line 97

def get_proteins
  var = 0
  @Content.each do |x|
    var +=  x.NutritionalInfo.Proteins
  end
  var
end

#get_vctObject

returns the vct



70
71
72
73
74
75
76
# 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) ⇒ Object



27
28
29
30
31
# 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) ⇒ Object



23
24
25
# File 'lib/Prct07/Menu.rb', line 23

def porcentage pctg
  @Porcentage = check_porcentage pctg
end

#to_sObject

necessary for puts method



106
107
108
109
110
111
112
113
# 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