Class: Menu
Overview
Direct Known Subclasses
Instance Attribute Summary collapse
-
#Content ⇒ Object
readonly
Returns the value of attribute Content.
-
#Etiqueta ⇒ Object
readonly
Returns the value of attribute Etiqueta.
-
#Porcentage ⇒ Object
readonly
Returns the value of attribute Porcentage.
-
#Title ⇒ Object
readonly
Returns the value of attribute Title.
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
necessary for comparable module.
-
#check_content(content) ⇒ Object
checks if the content is an array of plates (or derivated classes).
-
#check_name(title) ⇒ Object
checks if the name match any of the allowed ones.
-
#check_porcentage(porcentage) ⇒ Object
checks if the porcentage is a valid number [0, 100].
- #etiqueta(etiq) ⇒ Object
-
#get_fats ⇒ Object
returns the total calories of all plates.
-
#get_hidrates ⇒ Object
returns the total hidrates of all plates.
-
#get_proteins ⇒ Object
returns the total proteins of all plates.
-
#get_vct ⇒ Object
returns the vct.
-
#initialize(title = "", &block) ⇒ Menu
constructor
A new instance of Menu.
- #plato(plato) ⇒ Object
- #porcentage(pctg) ⇒ Object
-
#to_s ⇒ Object
necessary for puts method.
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
#Content ⇒ Object (readonly)
Returns the value of attribute Content.
4 5 6 |
# File 'lib/Prct07/Menu.rb', line 4 def Content @Content end |
#Etiqueta ⇒ Object (readonly)
Returns the value of attribute Etiqueta.
4 5 6 |
# File 'lib/Prct07/Menu.rb', line 4 def Etiqueta @Etiqueta end |
#Porcentage ⇒ Object (readonly)
Returns the value of attribute Porcentage.
4 5 6 |
# File 'lib/Prct07/Menu.rb', line 4 def Porcentage @Porcentage end |
#Title ⇒ Object (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_fats ⇒ Object
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_hidrates ⇒ Object
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_proteins ⇒ Object
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_vct ⇒ Object
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_s ⇒ Object
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 |