Class: AgeJp::Calculator
- Inherits:
-
Object
- Object
- AgeJp::Calculator
- Defined in:
- lib/age_jp/calculator.rb
Instance Attribute Summary collapse
-
#birthday ⇒ Object
readonly
Returns the value of attribute birthday.
Instance Method Summary collapse
- #age_at(date = Date.current) ⇒ Object
- #age_jp_at(date = Date.current) ⇒ Object
- #east_asian_age_reckoning_at(date = Date.current) ⇒ Object
-
#initialize(birthday) ⇒ Calculator
constructor
A new instance of Calculator.
- #insurance_age_at(date = Date.current) ⇒ Object
Constructor Details
#initialize(birthday) ⇒ Calculator
Returns a new instance of Calculator.
5 6 7 |
# File 'lib/age_jp/calculator.rb', line 5 def initialize(birthday) @birthday = birthday end |
Instance Attribute Details
#birthday ⇒ Object (readonly)
Returns the value of attribute birthday.
3 4 5 |
# File 'lib/age_jp/calculator.rb', line 3 def birthday @birthday end |
Instance Method Details
#age_at(date = Date.current) ⇒ Object
9 10 11 12 13 |
# File 'lib/age_jp/calculator.rb', line 9 def age_at(date = Date.current) return unless valid_birthday? && valid_date?(date) calculate_age(date) end |
#age_jp_at(date = Date.current) ⇒ Object
15 16 17 18 19 |
# File 'lib/age_jp/calculator.rb', line 15 def age_jp_at(date = Date.current) return unless valid_birthday? && valid_date?(date) calculate_age_jp(date) end |
#east_asian_age_reckoning_at(date = Date.current) ⇒ Object
21 22 23 24 25 26 |
# File 'lib/age_jp/calculator.rb', line 21 def east_asian_age_reckoning_at(date = Date.current) return unless valid_birthday? && valid_date?(date) age = calculate_age(date) until_birthday_this_year?(date) ? age + 2 : age + 1 end |
#insurance_age_at(date = Date.current) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/age_jp/calculator.rb', line 28 def insurance_age_at(date = Date.current) return unless valid_birthday? && valid_date?(date) # date時点での満年齢を取得 age = calculate_age(date) # その年齢に到達した誕生日を取得 last_birthday = birthday.next_year(age) # 前回の誕生日から計算基準日までの月差分を取得 month_diff = (date.year * 12 + date.month) - (last_birthday.year * 12 + last_birthday.month) # 月差分だけlast_birthdayをmonths_sinceした日付と、dateを比較し、month_diffの値を調整する # ex) # - last_birthday: 1/25 # - date: 2/15 # の場合、month_diffは、1だが、実際には0ヶ月差分としたい # - last_birthday.months_since(month_diff) => 2/25 # として、date < last_birthday.months_since(month_diff) の場合は、month_diff -= 1 を月差分とする # # last_birthday date month_diff months_since後 adjusted_month_diff # 2/25 2/27 0 2/25 0 # 2/25 3/1 1 3/25 0 # 2/25 3/27 1 3/25 1 # 2/25 4/1 2 4/25 1 # 2/25 4/27 2 4/25 2 month_diff -= 1 if date < last_birthday.months_since(month_diff) age += 1 if month_diff > 6 age end |