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.num_days_to_budget = 5         # The number of days remaining in the
                                  # week, including the current day

cb.prior_days_calories = 3_524    # net calories from days 1 and 2

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

Instance Method Summary collapse

Methods included from CachedOutputsWithRecalculation

def_input, def_output, extended

Constructor Details

#initialize(diet_period: nil, source_data: nil) ⇒ CalorieBudgeter

Returns a new instance of CalorieBudgeter.



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

def initialize(diet_period: nil, source_data: nil)
  self.diet_period = diet_period if diet_period
  self.source_data = source_data if source_data
end

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



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

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



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

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



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

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

#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



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

def_output :daily_calorie_goal do
  (remaining_calories_this_week.to_f / num_days_to_budget).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



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

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)



121
122
123
# File 'lib/nutrition_calculator/calorie_budgeter.rb', line 121

def_output :net_calorie_consumption do
  calories_consumed - calories_burned
end

#num_days_to_budgetInteger

Returns The number of days across which to budget the remaining net calorie goal for the week. This includes the current day.

Returns:

  • (Integer)

    The number of days across which to budget the remaining net calorie goal for the week. This includes the current day.



98
99
100
# File 'lib/nutrition_calculator/calorie_budgeter.rb', line 98

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

#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).



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

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



150
151
152
# File 'lib/nutrition_calculator/calorie_budgeter.rb', line 150

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



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

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)



178
179
180
# File 'lib/nutrition_calculator/calorie_budgeter.rb', line 178

def_output :remaining_calories_this_week do
  weekly_calorie_goal - prior_days_calories
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



157
158
159
# File 'lib/nutrition_calculator/calorie_budgeter.rb', line 157

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



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

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



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

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



77
78
79
# File 'lib/nutrition_calculator/calorie_budgeter.rb', line 77

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

Instance Method Details

#diet_period=(diet_period) ⇒ Object



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

def diet_period=(diet_period)
  self.resting_metabolic_rate = diet_period.resting_metabolic_rate
  self.weekly_calorie_goal = diet_period.net_calorie_goal
  self.num_days_to_budget = diet_period.days_remaining
end

#source_data=(source_data) ⇒ Object



59
60
61
62
63
# File 'lib/nutrition_calculator/calorie_budgeter.rb', line 59

def source_data=(source_data)
  self.prior_days_calories = source_data.prior_days_net_calories
  self.calories_consumed = source_data.calories_consumed_today
  self.calories_burned = source_data.calories_burned_today
end