Class: NutritionCalculator::CalorieBudgeter

Inherits:
Object
  • Object
show all
Extended by:
CachedOutputsWithRecalculation
Defined in:
lib/nutrition_calculator/calorie_budgeter.rb

Overview

Calculates Calorie Budget Per Day in Weekly Context

The ‘NutritionCalculator::CalorieBudgeter` is used to determine how many calories you need to consume and how many calories you need to burn via exercise for a given day in order to stay on target with your diet. In particular, it ensures that you consume at least enough to satisfy your resting metabolic rate each day, even if that means you need to burn off calories via exercise to keep on track. It operates on a weekly basis, so if you are over-/under-budget on a given day, the goals for the remainder of the week will be adjusted accordingly.

Examples:

cb = NutritionCalculator::CalorieBudgeter.new

cb.resting_metabolic_rate = 2_000 # calories per day
cb.weekly_calorie_goal = 10_500 # creates an average deficit of 500 calories/day
cb.current_day_of_week = 3 # if your week starts on Monday, this would be Wednesday
cb.prior_days_calories = 3_524 # net calories from Monday and Tuesday

cb.calories_consumed = 0
cb.calories_burned = 0

cb.calories_remaining
#=> 2_000 

cb.exercise_calories_remaining
#=> 605

cb.calories_consumed = 681 # total calories consumed today
cb.calories_burned = 1752

cb.calories_remaining
#=> 2_466

cb.exercise_calories_remaining
#=> 0

Inputs collapse

Outputs collapse

Method Summary

Methods included from CachedOutputsWithRecalculation

def_input, def_output, extended

Instance Attribute Details

#calories_burnedInteger

Returns The total number of calories burned via exercise today.

Returns:

  • (Integer)

    The total number of calories burned via exercise today



95
96
97
98
# File 'lib/nutrition_calculator/calorie_budgeter.rb', line 95

def_input :calories_burned, validate_with: ->(value) {
  value.kind_of?(Integer) \
    && value >= 0
}

#calories_consumedInteger

Returns The total number of calories consumed today.

Returns:

  • (Integer)

    The total number of calories consumed today



88
89
90
91
# File 'lib/nutrition_calculator/calorie_budgeter.rb', line 88

def_input :calories_consumed, validate_with: ->(value) {
  value.kind_of?(Integer) \
    && value >= 0
}

#calories_remainingInteger (readonly)

Returns The number of calories remaining to consume today.

Returns:

  • (Integer)

    The number of calories remaining to consume today



111
112
113
# File 'lib/nutrition_calculator/calorie_budgeter.rb', line 111

def_output :calories_remaining do
  [0, remaining_to_target].max
end

#current_day_of_weekInteger

Returns The number of the day of the week.

Examples:

If you start your week on Monday

1 - Monday
2 - Tuesday
3 - Wednesday
4 - Thursday
5 - Friday
6 - Saturday
7 - Sunday

Returns:

  • (Integer)

    The number of the day of the week



82
83
84
# File 'lib/nutrition_calculator/calorie_budgeter.rb', line 82

def_input :current_day_of_week, validate_with: ->(value) {
  (1..7).include?(value)
}

#daily_calorie_goalInteger (readonly)

Returns The number of net calories that should be consumed today to meet the weekly calorie goal.

Returns:

  • (Integer)

    The number of net calories that should be consumed today to meet the weekly calorie goal



154
155
156
# File 'lib/nutrition_calculator/calorie_budgeter.rb', line 154

def_output :daily_calorie_goal do
  (remaining_calories_this_week.to_f / remaining_days_of_week).round
end

#exercise_calories_remainingInteger (readonly)

Returns The number of calories that must still be burned today in order to meet the daily calorie goal.

Returns:

  • (Integer)

    The number of calories that must still be burned today in order to meet the daily calorie goal



118
119
120
# File 'lib/nutrition_calculator/calorie_budgeter.rb', line 118

def_output :exercise_calories_remaining do
  [0, predicted_overage].max
end

#net_calorie_consumptionInteger (readonly)

Returns The net calories for the day (consumed - burned via exercise).

Returns:

  • (Integer)

    The net calories for the day (consumed - burned via exercise)



105
106
107
# File 'lib/nutrition_calculator/calorie_budgeter.rb', line 105

def_output :net_calorie_consumption do
  calories_consumed - calories_burned
end

#predicted_calorie_consumptionInteger (readonly)

Returns The number of calories that will likely be consumed today (the greater of actual consumption or target consumption).

Returns:

  • (Integer)

    The number of calories that will likely be consumed today (the greater of actual consumption or target consumption).



126
127
128
# File 'lib/nutrition_calculator/calorie_budgeter.rb', line 126

def_output :predicted_calorie_consumption do
  [target_daily_calorie_consumption, calories_consumed].max
end

#predicted_overageInteger (readonly)

Returns The number of calories consumed (or predicted to be consumed) that is greater than the daily calorie goal and not yet burned off via exercise.

Returns:

  • (Integer)

    The number of calories consumed (or predicted to be consumed) that is greater than the daily calorie goal and not yet burned off via exercise



134
135
136
# File 'lib/nutrition_calculator/calorie_budgeter.rb', line 134

def_output :predicted_overage do
  predicted_calorie_consumption - daily_calorie_goal - calories_burned
end

#prior_days_caloriesInteger

Returns The total net calories from all days this week prior to the current day.

Examples:

If it is currently Wednesday
And on Monday you consumed 100 calories and burned 75 for a net of 25
And on Tuesday you consumed 200 calories and burned 100 for a net of 100
Then you don't care about today's calories
And the value for this input should be 125 (Monday + Tuesday)

Returns:

  • (Integer)

    The total net calories from all days this week prior to the current day



68
69
70
# File 'lib/nutrition_calculator/calorie_budgeter.rb', line 68

def_input :prior_days_calories, validate_with: ->(value) {
  value.kind_of?(Integer)
}

#remaining_calories_this_weekInteger (readonly)

Returns The number of calories left in the calorie budget for the current week (does not include calories consumed today).

Returns:

  • (Integer)

    The number of calories left in the calorie budget for the current week (does not include calories consumed today)



162
163
164
# File 'lib/nutrition_calculator/calorie_budgeter.rb', line 162

def_output :remaining_calories_this_week do
  weekly_calorie_goal - prior_days_calories
end

#remaining_days_of_weekInteger (readonly)

Returns The number of days remaining in the week, including the current day.

Returns:

  • (Integer)

    The number of days remaining in the week, including the current day



169
170
171
# File 'lib/nutrition_calculator/calorie_budgeter.rb', line 169

def_output :remaining_days_of_week do
  8 - current_day_of_week
end

#remaining_to_targetInteger (readonly)

Returns The number of calories that must still be consumed to hit the day’s target.

Returns:

  • (Integer)

    The number of calories that must still be consumed to hit the day’s target



141
142
143
# File 'lib/nutrition_calculator/calorie_budgeter.rb', line 141

def_output :remaining_to_target do
  target_daily_calorie_consumption - calories_consumed
end

#resting_metabolic_rateInteger

Returns The daily resting metabolic rate in calories.

Returns:

  • (Integer)

    The daily resting metabolic rate in calories



47
48
49
50
# File 'lib/nutrition_calculator/calorie_budgeter.rb', line 47

def_input :resting_metabolic_rate, validate_with: ->(value) {
  value.kind_of?(Integer) \
    && value > 0
}

#target_daily_calorie_consumptionInteger (readonly)

Returns The number of calories that should be consumed today.

Returns:

  • (Integer)

    The number of calories that should be consumed today



147
148
149
# File 'lib/nutrition_calculator/calorie_budgeter.rb', line 147

def_output :target_daily_calorie_consumption do
  [(daily_calorie_goal + calories_burned), resting_metabolic_rate].max
end

#weekly_calorie_goalInteger

Returns The total net calories (consumed - burned) planned for the week.

Returns:

  • (Integer)

    The total net calories (consumed - burned) planned for the week



55
56
57
# File 'lib/nutrition_calculator/calorie_budgeter.rb', line 55

def_input :weekly_calorie_goal, validate_with: ->(value) {
  value.kind_of?(Integer)
}