Class: Happybirthday::Age

Inherits:
Base
  • Object
show all
Defined in:
lib/happybirthday/age.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(birthday:, present_day: Date.today) ⇒ Happybirthday::Age

Happybirthday::Age initializer

Parameters:

  • birthday (Happybirthday::Birtyday)

    birthday object

  • present_day (Date) (defaults to: Date.today)

    a day for calculating age



9
10
11
12
# File 'lib/happybirthday/age.rb', line 9

def initialize(birthday:, present_day: Date.today)
  @birthday = birthday
  @present_day = present_day
end

Instance Attribute Details

#birthdayObject (readonly)

Returns the value of attribute birthday.



3
4
5
# File 'lib/happybirthday/age.rb', line 3

def birthday
  @birthday
end

#present_dayObject (readonly)

Returns the value of attribute present_day.



3
4
5
# File 'lib/happybirthday/age.rb', line 3

def present_day
  @present_day
end

Instance Method Details

#at(date) ⇒ Happybirthday::Age

Get Happybirthday::Age object which has specific date you passed

Parameters:

  • date (Date, String)

    Date or date like String (ex.“2018-05-08”)

Returns:



26
27
28
# File 'lib/happybirthday/age.rb', line 26

def at(date)
  Age.new(birthday: birthday, present_day: to_date(date))
end

#years_after(year = 0) ⇒ Happybirthday::Age

Get Happybirthday::Age object which has future date

Parameters:

  • year (Integer) (defaults to: 0)

    number of years

Returns:



40
41
42
# File 'lib/happybirthday/age.rb', line 40

def years_after(year = 0)
  Age.new(birthday: birthday, present_day: present_day.next_year(year))
end

#years_before(year = 0) ⇒ Happybirthday::Age

Get Happybirthday::Age object which has past date

Parameters:

  • year (Integer) (defaults to: 0)

    number of years

Returns:



33
34
35
# File 'lib/happybirthday/age.rb', line 33

def years_before(year = 0)
  Age.new(birthday: birthday, present_day: present_day.prev_year(year))
end

#years_oldInteger?

Get age

Returns:

  • (Integer)

    if Age.present_day is after birthday

  • (nil)

    if Age.present_day is before birthday



17
18
19
20
21
# File 'lib/happybirthday/age.rb', line 17

def years_old
  return nil if present_day < birthday.date
  format = "%Y%m%d"
  (present_day.strftime(format).to_i - birthday.date.strftime(format).to_i) / 10000
end