Module: Physical_activity_recorder

Defined in:
lib/physical-activity-recorder.rb,
lib/physical-activity-recorder/version.rb

Constant Summary collapse

VERSION =
'0.0.3'

Class Method Summary collapse

Class Method Details

.add_to_records(new_record, records) ⇒ void

This method returns an undefined value.

Add record.

Parameters:

  • ({String => (Fixnum, Fixnum, String)})
  • ({String => (Fixnum, Fixnum, String)})


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/physical-activity-recorder.rb', line 67

def add_to_records(new_record, records)
  activity_time = new_record.keys[0]
  if records.key?(activity_time)
    old = records[activity_time]
    new = (0..2).map do |i|
      old[i] + new_record[activity_time][i]
    end
    records[activity_time] = new
  # do not add empty records
  elsif new_record[activity_time].first(2).reduce(:+) == 0
    records
  else
    records[activity_time] = new_record[activity_time]
  end
end

.plan(current_time = Time.now, current_soft_end = Time.now, current_hard_end = Time.now, moderate_minutes = 0, vigorous_minutes = 0) ⇒ {Symbol => Time}

Plan physical activity

Parameters:

  • (Time)
  • (Time)
  • (Time)
  • (Fixnum)
  • (Fixnum)

Returns:

  • ({Symbol => Time})


112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/physical-activity-recorder.rb', line 112

def plan(current_time=Time.now, current_soft_end=Time.now, current_hard_end=Time.now, moderate_minutes=0, vigorous_minutes=0)
  # Activity should be done for at least 10 minutes at a time.
  if moderate_minutes + vigorous_minutes < 10
    hard_end = current_hard_end
    soft_end = current_soft_end
  else
    # With vigorous activities, you get similar health benefits in half the time it takes you with moderate ones.
    increment_time = moderate_minutes + vigorous_minutes * 2
    # United States Department of Agriculture recommends at least 2 hours and 30 minutes each week
    # of aerobic physical activity at a moderate level.
    # 2 hours and 30 minutes is 150 (2 * 60 + 30) minutes per week,
    # and we set the hard end based on 168 (24 * 7) minutes per week, i.e. 1 minute for 1 hour.
    # I chose 168 for implement simplicity.
    hard_end = current_hard_end + increment_time.hours

    # United States Department of Agriculture suggests
    # 5 or more hours activity per week can provide even more health benefits.
    # 5 hours is  300 (5 * 60) minutes per week,
    # and we set the soft end based on 336 (24 * 7 * 2) minutes per week, i.e. 2 minutes for 1 hour.
    # Again, I chose 336 for  implement simplicity.
    soft_end = current_soft_end + (increment_time / 2).to_i.hours
  end

  # soft end and hard end should not exceed 7 days (1 week).
  result = {soft: soft_end, hard: hard_end}
  result.each do |k, v|
    result[k] = if (v  - current_time).abs < 7.days
          v
        elsif v > current_time
          current_time + 7.days
        else
          current_time - 7.days
        end
  end
  result
end

.record(activity_time = Time.now, moderate_minutes = 0, vigorous_minutes = 0, activity_notes = '') ⇒ {String => (Fixnum, Fixnum, String)}

Record physical activity.

Parameters:

  • (Time)
  • (Fixnum)
  • (Fixnum)
  • (String)

Returns:

  • ({String => (Fixnum, Fixnum, String)})


37
38
39
# File 'lib/physical-activity-recorder.rb', line 37

def record(activity_time=Time.now, moderate_minutes=0, vigorous_minutes=0, activity_notes='')
  {activity_time.strftime('%F') => [moderate_minutes, vigorous_minutes, activity_notes]}
end