Class: Food
Direct Known Subclasses
CarbohydrateRich, EggMilkAndDerivatives, Fish, Fruits, MeatAndDerivatives, OilsAndSweets, Vegetables
Constant Summary
- DELTA_TIME =
Time interval between metrics (in minutes)
5.0
- @@nutrients_name =
Names of the nutrients
['proteins', 'carbohydrates', 'fats']
- @@nutients_energy_value =
Energy value (in Kcal) that each nutrient type gives
[4, 4, 9]
Instance Attribute Summary collapse
-
#energetic_value ⇒ Float
readonly
Total energetic value of the food.
-
#glycemic_index ⇒ Float | nil
readonly
Glycemic index for this food if it is calculated, nil otherwise.
-
#name ⇒ String
readonly
Name of the food.
-
#nutrient_quantities ⇒ Array<Float>
readonly
Quantity in grams of each nutrient.
Class Method Summary collapse
-
.nutrients_name ⇒ Array<String>
Getter for the nutrients name.
Instance Method Summary collapse
-
#<=>(o) ⇒ Fixnum
<=> operator, defined so instances could be comparable.
-
#gi(people_metrics) ⇒ Food
Calculates the glycemic index for the food given data of various people.
-
#iauc(food_metrics) ⇒ Float
Calculates the incremental area under the curve for this food given the glucose levels in blood for a person.
-
#individual_gi(food_metrics, glucose_metrics) ⇒ Float
Calculates the glycemic index of a person for the given food.
-
#initialize(name, nutrient_quantities) ⇒ Food
constructor
A new instance of Food.
-
#quantity_of_nutrient(n) ⇒ Float
Given the name of a nutrient, #quantity_of_nutrient returns the quantity (in grams) of that nutrient.
-
#to_s ⇒ String
to_s method.
Constructor Details
#initialize(name, nutrient_quantities) ⇒ Food
Returns a new instance of Food
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/TDD/Food.rb', line 30 def initialize( name, nutrient_quantities ) raise( ArgumentError, 'Invalid number of nutrients values' ) unless (nutrient_quantities.length == @@nutrients_name.length) @name = name.downcase # How much of each nutrient this food has @nutrient_quantities = nutrient_quantities # Total energetic value of this food instance @energetic_value = 0 @nutrient_quantities.length.times do |i| @energetic_value += @nutrient_quantities[i] * @@nutients_energy_value[i] end # Setting the glycemic index to a default value @glycemic_index = nil end |
Instance Attribute Details
#energetic_value ⇒ Float (readonly)
Returns Total energetic value of the food
23 24 25 |
# File 'lib/TDD/Food.rb', line 23 def energetic_value @energetic_value end |
#glycemic_index ⇒ Float | nil (readonly)
Returns Glycemic index for this food if it is calculated, nil otherwise
26 27 28 |
# File 'lib/TDD/Food.rb', line 26 def glycemic_index @glycemic_index end |
#name ⇒ String (readonly)
Returns Name of the food
17 18 19 |
# File 'lib/TDD/Food.rb', line 17 def name @name end |
#nutrient_quantities ⇒ Array<Float> (readonly)
Returns Quantity in grams of each nutrient
20 21 22 |
# File 'lib/TDD/Food.rb', line 20 def nutrient_quantities @nutrient_quantities end |
Class Method Details
.nutrients_name ⇒ Array<String>
Getter for the nutrients name
9 10 11 |
# File 'lib/TDD/Food.rb', line 9 def self.nutrients_name @@nutrients_name end |
Instance Method Details
#<=>(o) ⇒ Fixnum
We consider two foods to be equal if their energetic value is the samew
<=> operator, defined so instances could be comparable
71 72 73 |
# File 'lib/TDD/Food.rb', line 71 def <=>(o) return self.energetic_value <=> o.energetic_value end |
#gi(people_metrics) ⇒ Food
Calculates the glycemic index for the food given data of various people
100 101 102 103 104 105 106 107 108 |
# File 'lib/TDD/Food.rb', line 100 def gi(people_metrics) people_igs = [] people_metrics.each do |person_metrics| people_igs << individual_gi(person_metrics[:food_metrics], person_metrics[:glucose_metrics]) end @glycemic_index = people_igs.reduce(0.0, :+) / people_igs.length return self end |
#iauc(food_metrics) ⇒ Float
Calculates the incremental area under the curve for this food given the glucose levels in blood for a person.
81 82 83 84 85 |
# File 'lib/TDD/Food.rb', line 81 def iauc(food_metrics) raise ArgumentError.new('Need at least 2 values to calculate the first trapezoid area') unless (food_metrics.length > 1) DELTA_TIME/2.0 * (food_metrics.drop(1).reduce(0.0, :+) + food_metrics.take(food_metrics.length - 1).reduce(0.0, :+) - 2.0 * food_metrics[0] * food_metrics.length) end |
#individual_gi(food_metrics, glucose_metrics) ⇒ Float
Calculates the glycemic index of a person for the given food
91 92 93 94 95 |
# File 'lib/TDD/Food.rb', line 91 def individual_gi(food_metrics, glucose_metrics) raise ArgumentError.new("Sizes don't match") unless food_metrics.length == glucose_metrics.length iauc(food_metrics) / iauc(glucose_metrics) * 100.0 end |
#quantity_of_nutrient(n) ⇒ Float
Given the name of a nutrient, #quantity_of_nutrient returns the quantity (in grams) of that nutrient
50 51 52 53 54 55 56 |
# File 'lib/TDD/Food.rb', line 50 def quantity_of_nutrient( n ) # index method returns nil if the element doesn't exist index = @@nutrients_name.index( n ) raise( ArgumentError, 'Invalid nutrient name' ) unless index return @nutrient_quantities[ index ] end |
#to_s ⇒ String
to_s method
60 61 62 63 64 65 66 |
# File 'lib/TDD/Food.rb', line 60 def to_s result = @name + ': ' (@nutrient_quantities.length - 1).times do |i| result += "#{@nutrient_quantities[i]}g of #{@@nutrients_name[i]}, " end result += "#{@nutrient_quantities[-1]}g of #{@@nutrients_name[-1]}" end |