Class: AgeService

Inherits:
Object
  • Object
show all
Defined in:
app/services/age_service.rb

Overview

Calculates a person's age given their birthday relative to midnight today unless another relative date is specified in the from: parameter

Class Method Summary collapse

Class Method Details

.call(date_of_birth:, from: Time.zone.today) ⇒ Integer

Returns The person's age in years

Parameters:

  • date_of_birth (Date)

    The person's date of birth

  • from (Date)

    The date to calculate from (defaults to today)

Returns:

  • (Integer)

    The person's age in years


9
10
11
12
13
14
15
# File 'app/services/age_service.rb', line 9

def self.call(date_of_birth:, from: Time.zone.today)
  if before_this_years_birthday?(date_of_birth: date_of_birth, from: from)
    from.year - date_of_birth.year - 1
  else
    from.year - date_of_birth.year
  end
end