# # [Class to representate diets menus of day and week] # # @author [julian] # class Menu attr_accessor :day, :title # # [initialize] # @param day [type] [description] # @param &block [type] [description] # # @return [type] [description] 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 def titulo(titulo) @title = titulo end def ingesta(properties = {}) @min = properties[:min] ? properties[:min] : "unknown" @max = properties[:max] ? properties[:max] : "unknown" end def desayuno(properties = {}) @breakfast.push(food(properties)) end def almuerzo(properties = {}) @lunch.push(food(properties)) end def cena(properties = {}) @dinner.push(food(properties)) end 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 # # [turns the given list into a formated string] # @param liste [list] [list of menu] # # @return [string] [readable string] 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 # # [method to represent the created obj as a string, readable for humans] # # @return [string] [obj as a string, readable] 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 end