Class: Etiqueta

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/InformacionNutricional/etiqueta.rb

Overview

Esta clase implementa una etiqueta que almacena el valor nutricional de alimentos Se ha incluido el mixin Comparable

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, portions, n_portions, portion_size, fats, s_fats, carbs, sugar, protein, salt) ⇒ Etiqueta

Se genera la etiqueta con los datos del alimento proporcionados



16
17
18
# File 'lib/InformacionNutricional/etiqueta.rb', line 16

def initialize (name, portions, n_portions, portion_size, fats, s_fats, carbs, sugar, protein, salt)
	@name, @portions, @n_portions, @portion_size, @fats, @s_fats, @carbs, @sugar, @protein, @salt = name, portions, n_portions, portion_size, fats, s_fats, carbs, sugar, protein, salt
end

Instance Attribute Details

#carbsObject (readonly)

Getters de las variables de instancia



13
14
15
# File 'lib/InformacionNutricional/etiqueta.rb', line 13

def carbs
  @carbs
end

#fatsObject (readonly)

Getters de las variables de instancia



13
14
15
# File 'lib/InformacionNutricional/etiqueta.rb', line 13

def fats
  @fats
end

#kcalObject (readonly)

Getters de las variables de instancia



13
14
15
# File 'lib/InformacionNutricional/etiqueta.rb', line 13

def kcal
  @kcal
end

#kjObject (readonly)

Getters de las variables de instancia



13
14
15
# File 'lib/InformacionNutricional/etiqueta.rb', line 13

def kj
  @kj
end

#n_portionsObject (readonly)

Getters de las variables de instancia



13
14
15
# File 'lib/InformacionNutricional/etiqueta.rb', line 13

def n_portions
  @n_portions
end

#nameObject (readonly)

Getters de las variables de instancia



13
14
15
# File 'lib/InformacionNutricional/etiqueta.rb', line 13

def name
  @name
end

#portion_sizeObject (readonly)

Getters de las variables de instancia



13
14
15
# File 'lib/InformacionNutricional/etiqueta.rb', line 13

def portion_size
  @portion_size
end

#portionsObject (readonly)

Getters de las variables de instancia



13
14
15
# File 'lib/InformacionNutricional/etiqueta.rb', line 13

def portions
  @portions
end

#proteinObject (readonly)

Getters de las variables de instancia



13
14
15
# File 'lib/InformacionNutricional/etiqueta.rb', line 13

def protein
  @protein
end

#s_fatsObject (readonly)

Getters de las variables de instancia



13
14
15
# File 'lib/InformacionNutricional/etiqueta.rb', line 13

def s_fats
  @s_fats
end

#saltObject (readonly)

Getters de las variables de instancia



13
14
15
# File 'lib/InformacionNutricional/etiqueta.rb', line 13

def salt
  @salt
end

#sugarObject (readonly)

Getters de las variables de instancia



13
14
15
# File 'lib/InformacionNutricional/etiqueta.rb', line 13

def sugar
  @sugar
end

Instance Method Details

#<=>(other) ⇒ Object

Se define para incluir el mixin Comparable Se toma como valor para la comparación los kJ (Valor que devuelve la función calculate_calories)



38
39
40
41
# File 'lib/InformacionNutricional/etiqueta.rb', line 38

def <=> (other)
	return nil unless other.is_a?Etiqueta
	calculate_calories <=> other.calculate_calories
end

#==(other) ⇒ Object

Override del == Se toman para considerar iguales: kcal, grasas, carbohidratos, proteínas y sal



46
47
48
49
50
51
52
53
# File 'lib/InformacionNutricional/etiqueta.rb', line 46

def == (other)
	return nil unless other.is_a?Etiqueta
	@kcal == other.kcal
	@fats == other.fats
	@carbs == other.carbs
	@protein == other.protein
	@salt == other.salt
end

#calculate_caloriesObject

Función que gendetermina las kilocalorías y los kilojulios de los alimentos



21
22
23
24
# File 'lib/InformacionNutricional/etiqueta.rb', line 21

def calculate_calories
	@kj = ((@fats * 37) + (@carbs * 17) + (@protein * 17) + (@salt * 28)).round(2)
	@kcal = ((@fats * 9) + (@carbs * 4) + (@protein * 4) + (@salt * 6)).round(2)
end

#to_sObject

Override del to_s



27
28
29
30
31
32
33
# File 'lib/InformacionNutricional/etiqueta.rb', line 27

def to_s
	s = ""
	if @portions == true
		s = "(#{@n_portions} porciones; #{@portion_size}g/porcion)"
	end
	"#{@name.upcase}#{s}: Por 100g - IR; VALOR ENERGETICO: #{@kcal} kcal/#{@kj} kJ - 2000 kcal/8400 kJ; GRASAS: #{@fats}g - 70g; SATURADAS: #{@s_fats}g - 20g; CARBOHIDRATOS: #{@carbs}g - 260g; AZUCARES: #{@sugar}g - 90g; PROTEINAS: #{@protein}g - 50g; SAL: #{@salt}g - 6g;"
end