age_jp 0.0.2 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +15 -0
- data/README.md +10 -8
- data/age_jp.gemspec +1 -1
- data/lib/age_jp/calculator.rb +37 -23
- data/lib/age_jp/core_ext/date.rb +11 -3
- data/lib/age_jp/version.rb +1 -1
- data/spec/age_jp_spec.rb +60 -0
- metadata +5 -6
- data/.rspec +0 -3
- data/Gemfile.lock +0 -46
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6272b2d23018dac1ebbc09e95fba7451b5252577
|
4
|
+
data.tar.gz: 0d3539915128036711c665758edff451347cebcb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5abce402708d389e45665bccf2bb9399d64bf8ea659fc75a22378d0ad4f499ee2940c22d7b9a0ce4ca8e362800c3bb676d1fd7076066ce4ee9c378d0f2124d7f
|
7
|
+
data.tar.gz: 546cf36d8c61e9a0133f117fbe81fb394a4a2419c1bd9d71ee416d1556d93930be2c54b7e663bc0b40a691609243eee7c8d14fc92adfbe40ecf2f323f8cc97be
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -27,14 +27,16 @@ Or install it yourself as:
|
|
27
27
|
Timecop.freeze(Time.new(2014, 12, 31))
|
28
28
|
|
29
29
|
birthday = Date.new(2000, 1, 1)
|
30
|
-
birthday.age
|
31
|
-
birthday.age_at(
|
32
|
-
birthday.age_jp
|
33
|
-
birthday.age_jp_at(
|
34
|
-
birthday.east_asian_age_reckoning
|
35
|
-
birthday.east_asian_age_reckoning_at(
|
36
|
-
birthday.
|
37
|
-
birthday.
|
30
|
+
birthday.age # 14 (通常の年齢)
|
31
|
+
birthday.age_at(Date.today) # same as birthday.age_at
|
32
|
+
birthday.age_jp # 15 (日本の法律準拠)
|
33
|
+
birthday.age_jp_at(Date.today) # same as birthday.age_jp_at
|
34
|
+
birthday.east_asian_age_reckoning # 15 (数え年)
|
35
|
+
birthday.east_asian_age_reckoning_at(Date.today) # same as birthday.east_asian_age_reckoning_at
|
36
|
+
birthday.insurance_age # 14 (保険年齢)
|
37
|
+
birthday.insurance_age_at(Date.today) # same as birthday.insurance_age_at
|
38
|
+
birthday.to_years_old(17) # 2017/01/01. 17歳の誕生日を返却
|
39
|
+
birthday.to_years_old_jp(17) # 2016/12/31. 17歳の年齢加算日(日本の法律準拠)を返却
|
38
40
|
```
|
39
41
|
|
40
42
|
## Contributing
|
data/age_jp.gemspec
CHANGED
@@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
|
-
spec.add_dependency "activesupport"
|
21
|
+
spec.add_dependency "activesupport", '>= 3.2'
|
22
22
|
|
23
23
|
spec.add_development_dependency "bundler", "~> 1.7"
|
24
24
|
spec.add_development_dependency "rake", "~> 10.0"
|
data/lib/age_jp/calculator.rb
CHANGED
@@ -4,50 +4,64 @@ module AgeJp
|
|
4
4
|
@birthday = birthday
|
5
5
|
end
|
6
6
|
|
7
|
-
def
|
8
|
-
return unless valid_birthday?
|
9
|
-
|
10
|
-
calculate_age(today)
|
11
|
-
end
|
12
|
-
|
13
|
-
def age_at(date)
|
7
|
+
def age_at(date = today)
|
14
8
|
return unless valid_birthday? && valid_date?(date)
|
15
9
|
|
16
10
|
calculate_age(date)
|
17
11
|
end
|
18
12
|
|
19
|
-
def
|
20
|
-
return unless valid_birthday?
|
21
|
-
|
22
|
-
calculate_age_jp(today)
|
23
|
-
end
|
24
|
-
|
25
|
-
def age_jp_at(date)
|
13
|
+
def age_jp_at(date = today)
|
26
14
|
return unless valid_birthday? && valid_date?(date)
|
27
15
|
|
28
16
|
calculate_age_jp(date)
|
29
17
|
end
|
30
18
|
|
31
|
-
def
|
32
|
-
return unless valid_birthday?
|
19
|
+
def east_asian_age_reckoning_at(date = today)
|
20
|
+
return unless valid_birthday? && valid_date?(date)
|
33
21
|
|
34
|
-
age = calculate_age(
|
35
|
-
until_birthday_this_year?(
|
22
|
+
age = calculate_age(date)
|
23
|
+
until_birthday_this_year?(date) ? age + 2 : age + 1
|
36
24
|
end
|
37
25
|
|
38
|
-
def
|
26
|
+
def insurance_age_at(date = today)
|
39
27
|
return unless valid_birthday? && valid_date?(date)
|
40
28
|
|
29
|
+
# date時点での満年齢を取得
|
41
30
|
age = calculate_age(date)
|
42
|
-
|
31
|
+
|
32
|
+
# その年齢に到達した誕生日を取得
|
33
|
+
last_birthday = @birthday.next_year(age)
|
34
|
+
|
35
|
+
# 前回の誕生日から計算基準日までの月差分を取得
|
36
|
+
month_diff = (date.year * 12 + date.month) - (last_birthday.year * 12 + last_birthday.month)
|
37
|
+
|
38
|
+
# 月差分だけlast_birthdayをmonths_sinceした日付と、dateを比較し、month_diffの値を調整する
|
39
|
+
# ex)
|
40
|
+
# - last_birthday: 1/25
|
41
|
+
# - date: 2/15
|
42
|
+
# の場合、month_diffは、1だが、実際には0ヶ月差分としたい
|
43
|
+
# - last_birthday.months_since(month_diff) => 2/25
|
44
|
+
# として、date < last_birthday.months_since(month_diff) の場合は、month_diff -= 1 を月差分とする
|
45
|
+
#
|
46
|
+
# last_birthday date month_diff months_since後 adjusted_month_diff
|
47
|
+
# 2/25 2/27 0 2/25 0
|
48
|
+
# 2/25 3/1 1 3/25 0
|
49
|
+
# 2/25 3/27 1 3/25 1
|
50
|
+
# 2/25 4/1 2 4/25 1
|
51
|
+
# 2/25 4/27 2 4/25 2
|
52
|
+
month_diff -= 1 if date < last_birthday.months_since(month_diff)
|
53
|
+
|
54
|
+
age += 1 if month_diff > 6
|
55
|
+
|
56
|
+
age
|
43
57
|
end
|
44
58
|
|
59
|
+
private
|
60
|
+
|
45
61
|
def today
|
46
62
|
Date.today
|
47
63
|
end
|
48
64
|
|
49
|
-
private
|
50
|
-
|
51
65
|
def calculate_age_jp(date)
|
52
66
|
# 誕生日が閏日の場合は、日本の民法ではdateが閏年であろうとなかろうと、2/28に年齢加算される
|
53
67
|
# つまり、誕生日が閏日 かつ dateが2/27の場合は、閏年であろうと無かろうと、年齢加算しない
|
@@ -75,7 +89,7 @@ module AgeJp
|
|
75
89
|
end
|
76
90
|
|
77
91
|
def valid_date?(date)
|
78
|
-
|
92
|
+
fail ArgumentError, 'invalid date' unless date && date.is_a?(Date)
|
79
93
|
true
|
80
94
|
end
|
81
95
|
|
data/lib/age_jp/core_ext/date.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
class Date
|
2
2
|
def age
|
3
|
-
AgeJp::Calculator.new(self).
|
3
|
+
AgeJp::Calculator.new(self).age_at
|
4
4
|
end
|
5
5
|
|
6
6
|
def age_at(date)
|
@@ -8,7 +8,7 @@ class Date
|
|
8
8
|
end
|
9
9
|
|
10
10
|
def age_jp
|
11
|
-
AgeJp::Calculator.new(self).
|
11
|
+
AgeJp::Calculator.new(self).age_jp_at
|
12
12
|
end
|
13
13
|
|
14
14
|
def age_jp_at(date)
|
@@ -16,13 +16,21 @@ class Date
|
|
16
16
|
end
|
17
17
|
|
18
18
|
def east_asian_age_reckoning
|
19
|
-
AgeJp::Calculator.new(self).
|
19
|
+
AgeJp::Calculator.new(self).east_asian_age_reckoning_at
|
20
20
|
end
|
21
21
|
|
22
22
|
def east_asian_age_reckoning_at(date)
|
23
23
|
AgeJp::Calculator.new(self).east_asian_age_reckoning_at(date)
|
24
24
|
end
|
25
25
|
|
26
|
+
def insurance_age
|
27
|
+
AgeJp::Calculator.new(self).insurance_age_at
|
28
|
+
end
|
29
|
+
|
30
|
+
def insurance_age_at(date)
|
31
|
+
AgeJp::Calculator.new(self).insurance_age_at(date)
|
32
|
+
end
|
33
|
+
|
26
34
|
alias_method :to_years_old, :next_year
|
27
35
|
|
28
36
|
def to_years_old_jp(n)
|
data/lib/age_jp/version.rb
CHANGED
data/spec/age_jp_spec.rb
CHANGED
@@ -76,6 +76,14 @@ describe AgeJp do
|
|
76
76
|
it { expect(age_at).to eq 17 }
|
77
77
|
end
|
78
78
|
end
|
79
|
+
|
80
|
+
context 'invalid date' do
|
81
|
+
let(:birthday) { Date.new(2000, 1, 1) }
|
82
|
+
let(:invalid_date) { 'String' }
|
83
|
+
subject(:age_at) { birthday.age_at(invalid_date) }
|
84
|
+
|
85
|
+
it { expect { subject }.to raise_error(/invalid date/) }
|
86
|
+
end
|
79
87
|
end
|
80
88
|
|
81
89
|
describe '#age_jp' do
|
@@ -285,4 +293,56 @@ describe AgeJp do
|
|
285
293
|
end
|
286
294
|
end
|
287
295
|
end
|
296
|
+
|
297
|
+
describe '#insurance_age' do
|
298
|
+
context "birthday is 2000/01/01. " do
|
299
|
+
let(:birthday) { Date.new(2000, 1, 1) }
|
300
|
+
subject(:insurance_age) { birthday.insurance_age }
|
301
|
+
|
302
|
+
context 'when today is 2015/01/01' do
|
303
|
+
before { Timecop.freeze(Date.new(2015, 1, 1)) }
|
304
|
+
|
305
|
+
it { expect(insurance_age).to eq 15 }
|
306
|
+
end
|
307
|
+
|
308
|
+
context 'when today is 2015/07/31' do
|
309
|
+
before { Timecop.freeze(Date.new(2015, 7, 31)) }
|
310
|
+
|
311
|
+
it { expect(insurance_age).to eq 15 }
|
312
|
+
end
|
313
|
+
|
314
|
+
context 'when today is 2015/08/01' do
|
315
|
+
before { Timecop.freeze(Date.new(2015, 8, 1)) }
|
316
|
+
|
317
|
+
it { expect(insurance_age).to eq 16 }
|
318
|
+
end
|
319
|
+
|
320
|
+
after { Timecop.return }
|
321
|
+
end
|
322
|
+
|
323
|
+
context "birthday is 2000/07/31. " do
|
324
|
+
let(:birthday) { Date.new(2000, 7, 31) }
|
325
|
+
subject(:insurance_age) { birthday.insurance_age }
|
326
|
+
|
327
|
+
context 'when today is 2016/02/28' do
|
328
|
+
before { Timecop.freeze(Date.new(2016, 2, 28)) }
|
329
|
+
|
330
|
+
it { expect(insurance_age).to eq 15 }
|
331
|
+
end
|
332
|
+
|
333
|
+
context 'when today is 2016/02/29' do
|
334
|
+
before { Timecop.freeze(Date.new(2016, 2, 29)) }
|
335
|
+
|
336
|
+
it { expect(insurance_age).to eq 16 }
|
337
|
+
end
|
338
|
+
|
339
|
+
context 'when today is 2016/03/01' do
|
340
|
+
before { Timecop.freeze(Date.new(2016, 3, 1)) }
|
341
|
+
|
342
|
+
it { expect(insurance_age).to eq 16 }
|
343
|
+
end
|
344
|
+
|
345
|
+
after { Timecop.return }
|
346
|
+
end
|
347
|
+
end
|
288
348
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: age_jp
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ryoff
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-08-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '3.2'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '3.2'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -88,9 +88,7 @@ extensions: []
|
|
88
88
|
extra_rdoc_files: []
|
89
89
|
files:
|
90
90
|
- ".gitignore"
|
91
|
-
- ".rspec"
|
92
91
|
- Gemfile
|
93
|
-
- Gemfile.lock
|
94
92
|
- LICENSE.txt
|
95
93
|
- README.md
|
96
94
|
- Rakefile
|
@@ -128,3 +126,4 @@ summary: A simpel age calculator for a law of Japan.
|
|
128
126
|
test_files:
|
129
127
|
- spec/age_jp_spec.rb
|
130
128
|
- spec/spec_helper.rb
|
129
|
+
has_rdoc:
|
data/.rspec
DELETED
data/Gemfile.lock
DELETED
@@ -1,46 +0,0 @@
|
|
1
|
-
PATH
|
2
|
-
remote: .
|
3
|
-
specs:
|
4
|
-
age_jp (0.0.2)
|
5
|
-
activesupport
|
6
|
-
|
7
|
-
GEM
|
8
|
-
remote: https://rubygems.org/
|
9
|
-
specs:
|
10
|
-
activesupport (4.2.1)
|
11
|
-
i18n (~> 0.7)
|
12
|
-
json (~> 1.7, >= 1.7.7)
|
13
|
-
minitest (~> 5.1)
|
14
|
-
thread_safe (~> 0.3, >= 0.3.4)
|
15
|
-
tzinfo (~> 1.1)
|
16
|
-
diff-lcs (1.2.5)
|
17
|
-
i18n (0.7.0)
|
18
|
-
json (1.8.2)
|
19
|
-
minitest (5.5.1)
|
20
|
-
rake (10.4.0)
|
21
|
-
rspec (3.1.0)
|
22
|
-
rspec-core (~> 3.1.0)
|
23
|
-
rspec-expectations (~> 3.1.0)
|
24
|
-
rspec-mocks (~> 3.1.0)
|
25
|
-
rspec-core (3.1.7)
|
26
|
-
rspec-support (~> 3.1.0)
|
27
|
-
rspec-expectations (3.1.2)
|
28
|
-
diff-lcs (>= 1.2.0, < 2.0)
|
29
|
-
rspec-support (~> 3.1.0)
|
30
|
-
rspec-mocks (3.1.3)
|
31
|
-
rspec-support (~> 3.1.0)
|
32
|
-
rspec-support (3.1.2)
|
33
|
-
thread_safe (0.3.5)
|
34
|
-
timecop (0.7.1)
|
35
|
-
tzinfo (1.2.2)
|
36
|
-
thread_safe (~> 0.1)
|
37
|
-
|
38
|
-
PLATFORMS
|
39
|
-
ruby
|
40
|
-
|
41
|
-
DEPENDENCIES
|
42
|
-
age_jp!
|
43
|
-
bundler (~> 1.7)
|
44
|
-
rake (~> 10.0)
|
45
|
-
rspec
|
46
|
-
timecop (~> 0.7)
|