Class: Menu
- Inherits:
-
Object
- Object
- Menu
- Defined in:
- lib/prct06/menu.rb
Overview
- Class to representate diets menus of day and week
Instance Attribute Summary collapse
-
#day ⇒ Object
Returns the value of attribute day.
-
#title ⇒ Object
Returns the value of attribute title.
Instance Method Summary collapse
- #almuerzo(properties = {}) ⇒ Object
- #cena(properties = {}) ⇒ Object
- #desayuno(properties = {}) ⇒ Object
- #food(properties) ⇒ Object
- #ingesta(properties = {}) ⇒ Object
-
#initialize(day, &block) ⇒ type
constructor
[initialize].
- #titulo(titulo) ⇒ Object
-
#to_s ⇒ string
[method to represent the created obj as a string, readable for humans].
-
#to_s_formatter(liste) ⇒ string
[turns the given list into a formated string].
Constructor Details
#initialize(day, &block) ⇒ type
- initialize
15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/prct06/menu.rb', line 15 def initialize(day, &block) @day = day @title = title @breakfast, @lunch, @dinner = [], [], [] @table = [" ", "grasas carbohidratos", "proteínas", "fibra", "sal", "valor energético"] @tot_energy = 0 if block_given? if block.arity == 1 yield self else instance_eval(&block) end end end |
Instance Attribute Details
#day ⇒ Object
Returns the value of attribute day.
7 8 9 |
# File 'lib/prct06/menu.rb', line 7 def day @day end |
#title ⇒ Object
Returns the value of attribute title.
7 8 9 |
# File 'lib/prct06/menu.rb', line 7 def title @title end |
Instance Method Details
#almuerzo(properties = {}) ⇒ Object
43 44 45 |
# File 'lib/prct06/menu.rb', line 43 def almuerzo(properties = {}) @lunch.push(food(properties)) end |
#cena(properties = {}) ⇒ Object
47 48 49 |
# File 'lib/prct06/menu.rb', line 47 def cena(properties = {}) @dinner.push(food(properties)) end |
#desayuno(properties = {}) ⇒ Object
39 40 41 |
# File 'lib/prct06/menu.rb', line 39 def desayuno(properties = {}) @breakfast.push(food(properties)) end |
#food(properties) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/prct06/menu.rb', line 51 def food(properties) food = Array.new(7) food[0] = properties[:descripcion] ? "\"#{properties[:descripcion]}\"" : "unknown" food[0] = food[0].length > 23 ? "#{food[0][0,20]}..\"" : food[0] food[1] = properties[:grasas] ? properties[:grasas] : 0.0 food[2] = properties[:carbohidratos] ? properties[:carbohidratos] : 0.0 food[3] = properties[:proteinas] ? properties[:proteinas] : 0.0 food[4] = properties[:fibra] ? properties[:fibra] : 0.0 food[5] = properties[:sal] ? properties[:sal] : 0.0 food[6] = (food[1] * 36 + food[2] * 17 + food[3] * 17 + food[5] * 25).round(2) @tot_energy += food[6] return food end |
#ingesta(properties = {}) ⇒ Object
34 35 36 37 |
# File 'lib/prct06/menu.rb', line 34 def ingesta(properties = {}) @min = properties[:min] ? properties[:min] : "unknown" @max = properties[:max] ? properties[:max] : "unknown" end |
#titulo(titulo) ⇒ Object
30 31 32 |
# File 'lib/prct06/menu.rb', line 30 def titulo(titulo) @title = titulo end |
#to_s ⇒ string
- method to represent the created obj as a string, readable for humans
89 90 91 92 93 94 95 96 97 98 |
# File 'lib/prct06/menu.rb', line 89 def to_s() output = "#{@day}\t\t\t\t\tComposición nutricional\n" output << "=" * 100 + "\n" output << "\t\t\tgrasas\tcarbohidratos\tproteínas\tfibra\tsal\tvalor energético\n" output << "Desayuno\n" + to_s_formatter(@breakfast) + "\n" output << "Almuerzo\n" + to_s_formatter(@lunch) + "\n" output << "Cena\n" + to_s_formatter(@dinner) output << "Valor energético total #{@tot_energy}" output end |
#to_s_formatter(liste) ⇒ string
- turns the given list into a formated string
70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/prct06/menu.rb', line 70 def to_s_formatter(liste) to_ret = "" liste.each do |obj| to_ret << "#{obj[0]}\t" if obj[0].length < 16 to_ret << "\t" if obj[0].length < 8 to_ret << "\t" end end to_ret << "#{obj[1]}\t#{obj[2]}\t\t#{obj[3]}\t\t#{obj[4]}\t#{obj[5]}\t#{obj[6]}\n" end return to_ret end |