age_calculator 1.2.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +1 -2
- data/age_calculator.gemspec +3 -1
- data/lib/age_calculator/railtie.rb +1 -1
- data/lib/age_calculator/validator.rb +9 -3
- data/lib/age_calculator/version.rb +1 -1
- data/test/age_calculator_test.rb +8 -8
- data/test/age_validator_test.rb +12 -12
- data/test/load_models.rb +1 -1
- metadata +9 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3ec23a2a002af612bd84ba554a9c1bf35a08032e02bbecbd8a8695eaedae52f3
|
4
|
+
data.tar.gz: 897fb24248fd7152d0a8b08c8af98850bb1a93a6628fc00875bc279e163bbaae
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4ee2b116c5f08ad13faf57b404a4cea2b1b230a39b26813ffafd3fc34eeb40f325a9c3a68fb39033ca79f7c9ca18f3d96b8cdccf31720adbc5a90b1bc9c6c9a7
|
7
|
+
data.tar.gz: 401ffa5d7b051a08f8a586d1766a1c0293f68a78743de66da46825526b3469fe752e201e816f6aa941bddabf8412b498c7f9b9f0dfa460d5d140bcdd407b0338
|
data/README.md
CHANGED
@@ -49,6 +49,7 @@ For a basic usage:
|
|
49
49
|
|
50
50
|
```ruby
|
51
51
|
birthday = Date.new(2000,1,1)
|
52
|
+
|
52
53
|
ac = AgeCalculator.new(birthday)
|
53
54
|
|
54
55
|
ac.age
|
@@ -63,12 +64,10 @@ For a model with a validation on the age:
|
|
63
64
|
```ruby
|
64
65
|
class Adult < ActiveRecord::Base
|
65
66
|
validates :birthday, age: { over: 18 }
|
66
|
-
|
67
67
|
end
|
68
68
|
|
69
69
|
class Adult < ActiveRecord::Base
|
70
70
|
validates :birthday, age: { over: 18, asof: Date.today.beginning_of_year }
|
71
|
-
|
72
71
|
end
|
73
72
|
```
|
74
73
|
|
data/age_calculator.gemspec
CHANGED
@@ -18,7 +18,9 @@ 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.required_ruby_version = ">= 2.7.8"
|
22
|
+
|
21
23
|
spec.add_development_dependency 'rake'
|
22
24
|
spec.add_development_dependency 'minitest'
|
23
|
-
spec.add_development_dependency 'activemodel'
|
25
|
+
spec.add_development_dependency 'activemodel'
|
24
26
|
end
|
@@ -1,16 +1,22 @@
|
|
1
|
+
require 'pathname'
|
2
|
+
|
1
3
|
class AgeValidator < ActiveModel::EachValidator
|
2
4
|
COMPARATORS = { :over => :>=, :under => :<= }.freeze
|
3
5
|
|
4
6
|
def validate_each(record, attribute, value)
|
5
|
-
return record.errors.add(attribute, :blank
|
6
|
-
return record.errors.add(attribute, :not_date
|
7
|
+
return record.errors.add(attribute, :blank) if value.blank?
|
8
|
+
return record.errors.add(attribute, :not_date) unless value.is_a?(Date)
|
7
9
|
|
8
10
|
age = AgeCalculator.new(value).age(asof: options[:asof])
|
9
11
|
|
10
12
|
options.slice(*COMPARATORS.keys).each do |option_key, option_value|
|
11
13
|
unless age.send(COMPARATORS[option_key], option_value.to_i)
|
12
|
-
record.errors.add(attribute,
|
14
|
+
record.errors.add(attribute, I18n.t("errors.messages.age_#{option_key}", age: option_value))
|
13
15
|
end
|
14
16
|
end
|
15
17
|
end
|
18
|
+
|
19
|
+
def self.locales
|
20
|
+
Dir[Pathname.new(__FILE__).join('../../../config/locales/*.yml')]
|
21
|
+
end
|
16
22
|
end
|
data/test/age_calculator_test.rb
CHANGED
@@ -4,26 +4,26 @@ describe AgeCalculator do
|
|
4
4
|
it 'calculates age from birthday' do
|
5
5
|
travel_to Date.new(2018,1,2) do
|
6
6
|
ac = AgeCalculator.new Date.new(2000,1,1)
|
7
|
-
ac.age.must_equal 18
|
7
|
+
_(ac.age).must_equal 18
|
8
8
|
|
9
9
|
ac = AgeCalculator.new Date.new(2000,1,2)
|
10
|
-
ac.age.must_equal 18
|
10
|
+
_(ac.age).must_equal 18
|
11
11
|
|
12
12
|
ac = AgeCalculator.new Date.new(2000,1,3)
|
13
|
-
ac.age.must_equal 17
|
13
|
+
_(ac.age).must_equal 17
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
17
17
|
it 'calculates age based on a given date' do
|
18
18
|
ac = AgeCalculator.new Date.new(2000,1,2)
|
19
19
|
|
20
|
-
ac.age(asof: Date.new(2018,1,1)).must_equal 17
|
21
|
-
ac.age(asof: Date.new(2018,1,2)).must_equal 18
|
22
|
-
ac.age(asof: Date.new(2018,1,3)).must_equal 18
|
20
|
+
_(ac.age(asof: Date.new(2018,1,1))).must_equal 17
|
21
|
+
_(ac.age(asof: Date.new(2018,1,2))).must_equal 18
|
22
|
+
_(ac.age(asof: Date.new(2018,1,3))).must_equal 18
|
23
23
|
end
|
24
24
|
|
25
25
|
it 'raises an error with an invalid date' do
|
26
|
-
error = -> { AgeCalculator.new nil }.must_raise ArgumentError
|
27
|
-
error.message.must_match(/nil is not a date/)
|
26
|
+
error = _(-> { AgeCalculator.new nil }).must_raise ArgumentError
|
27
|
+
_(error.message).must_match(/nil is not a date/)
|
28
28
|
end
|
29
29
|
end
|
data/test/age_validator_test.rb
CHANGED
@@ -8,30 +8,30 @@ describe AgeValidator do
|
|
8
8
|
it 'validates age' do
|
9
9
|
travel_to Date.new(2018,1,2) do
|
10
10
|
@adult.birthday = Date.new(2000,1,1)
|
11
|
-
@adult.valid
|
11
|
+
_(@adult.valid?).must_equal true
|
12
12
|
|
13
13
|
@adult.birthday = Date.new(2000,1,2)
|
14
|
-
@adult.valid
|
14
|
+
_(@adult.valid?).must_equal true
|
15
15
|
|
16
16
|
@adult.birthday = Date.new(2000,1,3)
|
17
|
-
@adult.valid
|
18
|
-
@adult.errors.full_messages.first.must_equal 'Birthday must be over 18'
|
17
|
+
_(@adult.valid?).must_equal false
|
18
|
+
_(@adult.errors.full_messages.first).must_equal 'Birthday must be over 18'
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
22
22
|
it 'validates presence' do
|
23
23
|
travel_to Date.new(2018,1,2) do
|
24
24
|
@adult.birthday = nil
|
25
|
-
@adult.valid
|
26
|
-
@adult.errors.full_messages.first.must_equal "Birthday can't be blank"
|
25
|
+
_(@adult.valid?).must_equal false
|
26
|
+
_(@adult.errors.full_messages.first).must_equal "Birthday can't be blank"
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
30
30
|
it 'validates date' do
|
31
31
|
travel_to Date.new(2018,1,2) do
|
32
32
|
@adult.birthday = 'invalid'
|
33
|
-
@adult.valid
|
34
|
-
@adult.errors.full_messages.first.must_equal 'Birthday is not a valid date'
|
33
|
+
_(@adult.valid?).must_equal false
|
34
|
+
_(@adult.errors.full_messages.first).must_equal 'Birthday is not a valid date'
|
35
35
|
end
|
36
36
|
end
|
37
37
|
|
@@ -40,7 +40,7 @@ describe AgeValidator do
|
|
40
40
|
|
41
41
|
travel_to Date.new(2018,1,2) do
|
42
42
|
adult.birthday = nil
|
43
|
-
adult.valid
|
43
|
+
_(adult.valid?).must_equal true
|
44
44
|
end
|
45
45
|
end
|
46
46
|
|
@@ -48,12 +48,12 @@ describe AgeValidator do
|
|
48
48
|
adult = AdultAsOf.new
|
49
49
|
|
50
50
|
adult.birthday = Date.new(2000,1,1)
|
51
|
-
adult.valid
|
51
|
+
_(adult.valid?).must_equal true
|
52
52
|
|
53
53
|
adult.birthday = Date.new(2000,1,2)
|
54
|
-
adult.valid
|
54
|
+
_(adult.valid?).must_equal true
|
55
55
|
|
56
56
|
adult.birthday = Date.new(2000,1,3)
|
57
|
-
adult.valid
|
57
|
+
_(adult.valid?).must_equal false
|
58
58
|
end
|
59
59
|
end
|
data/test/load_models.rb
CHANGED
@@ -11,7 +11,7 @@ include ActiveSupport::Testing::TimeHelpers
|
|
11
11
|
|
12
12
|
# Locale
|
13
13
|
I18n.enforce_available_locales = true
|
14
|
-
I18n.load_path +=
|
14
|
+
I18n.load_path += AgeValidator.locales
|
15
15
|
|
16
16
|
class Adult
|
17
17
|
include ActiveModel::Validations
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: age_calculator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kenn Ejima
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-03-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -44,14 +44,14 @@ dependencies:
|
|
44
44
|
requirements:
|
45
45
|
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: '
|
47
|
+
version: '0'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
52
|
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: '
|
54
|
+
version: '0'
|
55
55
|
description: A simple age calculator that just works
|
56
56
|
email:
|
57
57
|
- kenn.ejima@gmail.com
|
@@ -79,7 +79,7 @@ homepage: https://github.com/kenn/age_calculator
|
|
79
79
|
licenses:
|
80
80
|
- MIT
|
81
81
|
metadata: {}
|
82
|
-
post_install_message:
|
82
|
+
post_install_message:
|
83
83
|
rdoc_options: []
|
84
84
|
require_paths:
|
85
85
|
- lib
|
@@ -87,16 +87,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
87
87
|
requirements:
|
88
88
|
- - ">="
|
89
89
|
- !ruby/object:Gem::Version
|
90
|
-
version:
|
90
|
+
version: 2.7.8
|
91
91
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
92
92
|
requirements:
|
93
93
|
- - ">="
|
94
94
|
- !ruby/object:Gem::Version
|
95
95
|
version: '0'
|
96
96
|
requirements: []
|
97
|
-
|
98
|
-
|
99
|
-
signing_key:
|
97
|
+
rubygems_version: 3.5.3
|
98
|
+
signing_key:
|
100
99
|
specification_version: 4
|
101
100
|
summary: A simple age calculator that just works
|
102
101
|
test_files:
|