Class: Food

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/TDD/Food.rb

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, nutrient_quantities) ⇒ Food

Returns a new instance of Food

Parameters:

  • name (String)

    Name of the food

  • nutrient_quantities (Array<FixNum>)

    An array with the values for the quantities of nutrients 0:grams of proteins, 1:grams of carbohydrates, 2:grams of fats

Raises:

  • (ArgumentError)


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_valueFloat (readonly)

Returns Total energetic value of the food

Returns:

  • (Float)

    Total energetic value of the food



23
24
25
# File 'lib/TDD/Food.rb', line 23

def energetic_value
  @energetic_value
end

#glycemic_indexFloat | nil (readonly)

Returns Glycemic index for this food if it is calculated, nil otherwise

Returns:

  • (Float | nil)

    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

#nameString (readonly)

Returns Name of the food

Returns:

  • (String)

    Name of the food



17
18
19
# File 'lib/TDD/Food.rb', line 17

def name
  @name
end

#nutrient_quantitiesArray<Float> (readonly)

Returns Quantity in grams of each nutrient

Returns:

  • (Array<Float>)

    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_nameArray<String>

Getter for the nutrients name

Returns:

  • (Array<String>)

    Array with the names of the nutrients



9
10
11
# File 'lib/TDD/Food.rb', line 9

def self.nutrients_name
    @@nutrients_name
end

Instance Method Details

#<=>(o) ⇒ Fixnum

Note:

We consider two foods to be equal if their energetic value is the samew

<=> operator, defined so instances could be comparable

Returns:

  • (Fixnum)


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

Parameters:

  • People (Array<Hash<Symbol, Array<Float>>>)

    metrics, each hash in the array is the data for a person, the hash has to have :food_metrics and :glucose_metrics keys which values are the arrays corresponding to the glucose in blood metric after the food and the pure glucose respectively.

Returns:

  • (Food)

    Returns selfs so other methods could be chained



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.

Parameters:

  • food_metrics (Array<Float>)

    Glucose in blood metrics of a person, taken each DELTA_TIME minutes for a given food

Returns:

  • (Float)

    Area under the curve of the metrics

Raises:

  • (ArgumentError)


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

Parameters:

  • glucose_metrics (Array<Float>)

    Glucose in blood metrics of a person, taken each DELTA_TIME minutes for pure glucose

  • food_metrics (Array<Float>)

    Glucose in blood metrics of a person, taken each DELTA_TIME minutes for a given food

Returns:

  • (Float)

    Glycemic index of a person for the given food

Raises:

  • (ArgumentError)


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

Parameters:

  • n (String)

    The name of the nutrient. The value has to be one of “proteins”, “carbohydrates”, “fats”

Returns:

  • (Float)

    The quantity (in grams) of the nutrient requested that the food has

Raises:

  • (ArgumentError)


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_sString

to_s method

Returns:

  • (String)

    The format is “<food_name>: <protein_quantity>g, <carbohydrates_quantity>g, <fat_quantity>g”



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