class Happybirthday::Age

Attributes

birthday[R]
present_day[R]

Public Class Methods

new(birthday:, present_day: Date.today) click to toggle source

Happybirthday::Age initializer @param birthday [Happybirthday::Birtyday] birthday object @param present_day [Date] a day for calculating age @return [Happybirthday::Age]

# File lib/happybirthday/age.rb, line 9
def initialize(birthday:, present_day: Date.today)
  @birthday = birthday
  @present_day = present_day
end

Public Instance Methods

at(date) click to toggle source

Get Happybirthday::Age object which has specific date you passed @param date [Date,String] Date or date like String (ex.“2018-05-08”) @return [Happybirthday::Age]

# File lib/happybirthday/age.rb, line 26
def at(date)
  Age.new(birthday: birthday, present_day: to_date(date))
end
years_after(year = 0) click to toggle source

Get Happybirthday::Age object which has future date @param year [Integer] number of years @return [Happybirthday::Age]

# 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) click to toggle source

Get Happybirthday::Age object which has past date @param year [Integer] number of years @return [Happybirthday::Age]

# 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_old() click to toggle source

Get age @return [Integer] if Age.present_day is after birthday @return [nil] if Age.present_day is before birthday

# 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