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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: aee3d23e9bb225c4c03eb269c6a14da8c0f02c2bc8f0b389e0df617523f468d7
4
- data.tar.gz: f5efaf19f902905a43cac4d84ffb3b3f2b69a4fa2d09a09fe3079041d3a7d399
3
+ metadata.gz: 3ec23a2a002af612bd84ba554a9c1bf35a08032e02bbecbd8a8695eaedae52f3
4
+ data.tar.gz: 897fb24248fd7152d0a8b08c8af98850bb1a93a6628fc00875bc279e163bbaae
5
5
  SHA512:
6
- metadata.gz: 3d764ffaef4eaab4c5021055098d5c28e3ddb1a9ece41893aae16e4731a85c355c19caee51fcfb4f631d1e83660682c53432c7b891239dfb63a84f877715d194
7
- data.tar.gz: 8a8b31faa16c1e51a77c4f06e69dac481b7e7115cacc839d54a1d284597cab40a0b769c3af9c79361a8a176942c4407ddcdb8b04f8130967e834ac264a48e937
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
 
@@ -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', '>= 4.1' # ActiveSupport::Testing::TimeHelpers
25
+ spec.add_development_dependency 'activemodel'
24
26
  end
@@ -3,7 +3,7 @@ class AgeCalculator
3
3
  initializer 'age_calculator.active_model' do |app|
4
4
  require 'age_calculator/validator'
5
5
 
6
- I18n.load_path += Pathname.new(__FILE__).dirname.parent.parent.glob('config/locales/*.yml')
6
+ I18n.load_path += AgeValidator.locales
7
7
  end
8
8
  end
9
9
  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, options) if value.blank?
6
- return record.errors.add(attribute, :not_date, options) unless value.is_a?(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, :"age_#{option_key}", options.merge(age: option_value))
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
@@ -1,3 +1,3 @@
1
1
  class AgeCalculator
2
- VERSION = '1.2.0'
2
+ VERSION = '2.0.0'
3
3
  end
@@ -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
@@ -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?.must_equal true
11
+ _(@adult.valid?).must_equal true
12
12
 
13
13
  @adult.birthday = Date.new(2000,1,2)
14
- @adult.valid?.must_equal true
14
+ _(@adult.valid?).must_equal true
15
15
 
16
16
  @adult.birthday = Date.new(2000,1,3)
17
- @adult.valid?.must_equal false
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?.must_equal false
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?.must_equal false
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?.must_equal true
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?.must_equal true
51
+ _(adult.valid?).must_equal true
52
52
 
53
53
  adult.birthday = Date.new(2000,1,2)
54
- adult.valid?.must_equal true
54
+ _(adult.valid?).must_equal true
55
55
 
56
56
  adult.birthday = Date.new(2000,1,3)
57
- adult.valid?.must_equal false
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 += Pathname.new(__FILE__).dirname.parent.glob('config/locales/*.yml')
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: 1.2.0
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: 2018-04-04 00:00:00.000000000 Z
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: '4.1'
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: '4.1'
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: '0'
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
- rubyforge_project:
98
- rubygems_version: 2.7.6
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: