Class: NutritionalCalculator::Food
- Inherits:
-
Object
- Object
- NutritionalCalculator::Food
- Includes:
- Comparable
- Defined in:
- lib/nutritional_calculator/food.rb
Overview
Esta clase representa a un alimento de manera abstracta. Contiene su nombre y su información nutricional. Se ha incluido el módulo Comparable.
Direct Known Subclasses
Constant Summary
- PROTEINS_VALUE =
valor nutricional de las proteinas: 4.0
4.0
- CARBOHYDRATE_VALUE =
Valor nutricional de los glúcidos: 4.0
4.0
- LIPIDS_VALUE =
Valor nutricional de los lípidos: 9.0
9.0
Instance Attribute Summary collapse
-
#carbohydrates ⇒ Object
readonly
Returns the value of attribute carbohydrates.
-
#lipids ⇒ Object
readonly
Returns the value of attribute lipids.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#proteins ⇒ Object
readonly
Returns the value of attribute proteins.
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
Método que permite hacer al alimento Comparable.
-
#get_nutritional_value ⇒ float
Método que devuelve el valor nutricional del alimento.
-
#initialize(name, proteins, carbohydrates, lipids) ⇒ Food
constructor
Constructor.
-
#to_s ⇒ String
Método que transforma el objeto en un String.
Constructor Details
#initialize(name, proteins, carbohydrates, lipids) ⇒ Food
Constructor.
28 29 30 31 32 33 |
# File 'lib/nutritional_calculator/food.rb', line 28 def initialize(name, proteins, carbohydrates, lipids) @name = name @proteins = proteins @carbohydrates = carbohydrates @lipids = lipids end |
Instance Attribute Details
#carbohydrates ⇒ Object (readonly)
Returns the value of attribute carbohydrates
19 20 21 |
# File 'lib/nutritional_calculator/food.rb', line 19 def carbohydrates @carbohydrates end |
#lipids ⇒ Object (readonly)
Returns the value of attribute lipids
19 20 21 |
# File 'lib/nutritional_calculator/food.rb', line 19 def lipids @lipids end |
#name ⇒ Object (readonly)
Returns the value of attribute name
19 20 21 |
# File 'lib/nutritional_calculator/food.rb', line 19 def name @name end |
#proteins ⇒ Object (readonly)
Returns the value of attribute proteins
19 20 21 |
# File 'lib/nutritional_calculator/food.rb', line 19 def proteins @proteins end |
Instance Method Details
#<=>(other) ⇒ Object
Método que permite hacer al alimento Comparable. En mi caso el criterio de comparación ha sido el valor nutricional del alimento.
58 59 60 |
# File 'lib/nutritional_calculator/food.rb', line 58 def <=> (other) get_nutritional_value <=> other.get_nutritional_value end |
#get_nutritional_value ⇒ float
Método que devuelve el valor nutricional del alimento. El valor nutricional se calcula como la suma del producto de los valores nutricionales de los macronutrientes por los gramos de dicho macronutriente presente en el alimento.
49 50 51 |
# File 'lib/nutritional_calculator/food.rb', line 49 def get_nutritional_value @proteins * PROTEINS_VALUE + @carbohydrates * CARBOHYDRATE_VALUE + @lipids * LIPIDS_VALUE end |
#to_s ⇒ String
Método que transforma el objeto en un String. De esta forma será formateado por pantalla.
39 40 41 |
# File 'lib/nutritional_calculator/food.rb', line 39 def to_s "#{@name} -> proteínas(#{@proteins}) glúcidos(#{@carbohydrates}) lípidos(#{@lipids})" end |