Skip to content

Commit 8f3b6ca

Browse files
committed
added Swimmer#age_for_category_range (specs still TODO)
1 parent 6c487ef commit 8f3b6ca

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

app/models/goggles_db/swimmer.rb

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module GogglesDb
44
#
55
# = GogglesDb::Swimmer
66
#
7-
# - version: 7-0.7.10
7+
# - version: 7-0.8.00
88
# - author: Steve A.
99
#
1010
class Swimmer < ApplicationRecord
@@ -65,14 +65,37 @@ class Swimmer < ApplicationRecord
6565
#-- ------------------------------------------------------------------------
6666
#++
6767

68-
# Returns the swimmer age (as a numeric value) for a given +date+.
68+
# Returns the approximate swimmer age (as a numeric value) for a given +date+.
6969
#
7070
# == Params
7171
# - <tt>date</tt>: the date for which the age must be computed; default: +today+.
7272
def age(date = Time.zone.today)
7373
date.year - year_of_birth
7474
end
7575

76+
# Computes the swimmer's age, possily adjusted for the proper current category age range (5-years x category),
77+
# according to the given meeting date.
78+
#
79+
# == Rationale:
80+
# Given that Championships usually start in the second half of an year and we only consider the
81+
# year of birth (as if the swimmer was born on the 1st of January of that year), the swimmer's age
82+
# must be rounded up by 1 if the session month is in the second half of the current year - implying
83+
# the first half of its season.
84+
#
85+
# === Example:
86+
# YOB: 2000, current_year: 2024, session_month: 7 => age 24 => category 'M20' (meeting falls inside ending part of season 2023-2024)
87+
# YOB: 2000, current_year: 2024, session_month: 9 => age 25 => category 'M25' (meeting falls inside starting part of season 2024-2025)
88+
#
89+
# == Params:
90+
# - <tt>meeting_date</tt>: the meeting +Date+ to be considered; default: +today+
91+
#
92+
# == Returns:
93+
# The swimmer's age (as a numeric value) for a given +date+.
94+
# Raises an error unless +meeting_date+ responds to #year & #month (a valid +Date+).
95+
def age_for_category_range(meeting_date = Time.zone.today)
96+
meeting_date.year - year_of_birth + (meeting_date.month > 8 ? 1 : 0)
97+
end
98+
7699
# Returns the array list of all the distinct team IDs associated
77100
# to the object row through the available Badges.
78101
# Returns an empty array when nothing is found.

0 commit comments

Comments
 (0)