age_calculator 1.1.0 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/README.md +17 -6
- data/age_calculator.gemspec +1 -1
- data/bin/console +9 -0
- data/lib/age_calculator.rb +6 -3
- data/lib/age_calculator/railtie.rb +1 -2
- data/lib/age_calculator/validator.rb +1 -1
- data/lib/age_calculator/version.rb +1 -1
- data/test/age_calculator_test.rb +17 -4
- data/test/age_validator_test.rb +20 -10
- data/test/load_models.rb +36 -0
- data/test/test_helper.rb +2 -33
- metadata +6 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: aee3d23e9bb225c4c03eb269c6a14da8c0f02c2bc8f0b389e0df617523f468d7
|
4
|
+
data.tar.gz: f5efaf19f902905a43cac4d84ffb3b3f2b69a4fa2d09a09fe3079041d3a7d399
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3d764ffaef4eaab4c5021055098d5c28e3ddb1a9ece41893aae16e4731a85c355c19caee51fcfb4f631d1e83660682c53432c7b891239dfb63a84f877715d194
|
7
|
+
data.tar.gz: 8a8b31faa16c1e51a77c4f06e69dac481b7e7115cacc839d54a1d284597cab40a0b769c3af9c79361a8a176942c4407ddcdb8b04f8130967e834ac264a48e937
|
data/README.md
CHANGED
@@ -12,14 +12,14 @@ Here's the simplest, cleanest way:
|
|
12
12
|
(today.to_s(:number).to_i - birthday.to_s(:number).to_i) / 10000
|
13
13
|
```
|
14
14
|
|
15
|
-
Suppose today is May 16,
|
15
|
+
Suppose today is May 16, 2018 and calculate the age of people whose birthdays are May 15, 2000 (should be 18 years old) and May 17, 2000 (should be 17 years old) respectively.
|
16
16
|
|
17
17
|
```ruby
|
18
|
-
>
|
18
|
+
> 20180516 - 20000517
|
19
19
|
=> 179999
|
20
20
|
> 179999 / 10000
|
21
21
|
=> 17
|
22
|
-
>
|
22
|
+
> 20180516 - 20000515
|
23
23
|
=> 180001
|
24
24
|
> 180001 / 10000
|
25
25
|
=> 18
|
@@ -27,6 +27,8 @@ Suppose today is May 16, 2014 and calculate the age of people whose birthdays ar
|
|
27
27
|
|
28
28
|
Couldn't be simpler. Note that Ruby truncates the decimal fraction.
|
29
29
|
|
30
|
+
AgeCalculator is a small library to do just that.
|
31
|
+
|
30
32
|
## Installation
|
31
33
|
|
32
34
|
Add this line to your application's Gemfile:
|
@@ -46,10 +48,14 @@ $ gem install age_calculator
|
|
46
48
|
For a basic usage:
|
47
49
|
|
48
50
|
```ruby
|
49
|
-
birthday = Date.new(
|
51
|
+
birthday = Date.new(2000,1,1)
|
52
|
+
ac = AgeCalculator.new(birthday)
|
53
|
+
|
54
|
+
ac.age
|
55
|
+
=> 18
|
50
56
|
|
51
|
-
|
52
|
-
|
57
|
+
ac.age(asof: Date.new(2020,1,1))
|
58
|
+
=> 20
|
53
59
|
```
|
54
60
|
|
55
61
|
For a model with a validation on the age:
|
@@ -58,6 +64,11 @@ For a model with a validation on the age:
|
|
58
64
|
class Adult < ActiveRecord::Base
|
59
65
|
validates :birthday, age: { over: 18 }
|
60
66
|
|
67
|
+
end
|
68
|
+
|
69
|
+
class Adult < ActiveRecord::Base
|
70
|
+
validates :birthday, age: { over: 18, asof: Date.today.beginning_of_year }
|
71
|
+
|
61
72
|
end
|
62
73
|
```
|
63
74
|
|
data/age_calculator.gemspec
CHANGED
@@ -14,7 +14,7 @@ Gem::Specification.new do |spec|
|
|
14
14
|
spec.license = 'MIT'
|
15
15
|
|
16
16
|
spec.files = `git ls-files -z`.split("\x0")
|
17
|
-
spec.executables = spec.files.grep(%r{^
|
17
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
19
|
spec.require_paths = ['lib']
|
20
20
|
|
data/bin/console
ADDED
data/lib/age_calculator.rb
CHANGED
@@ -5,11 +5,14 @@ require 'age_calculator/railtie' if defined?(Rails)
|
|
5
5
|
class AgeCalculator
|
6
6
|
def initialize(birthday)
|
7
7
|
@birthday = birthday
|
8
|
+
|
9
|
+
unless @birthday.is_a?(Date)
|
10
|
+
fail ArgumentError, "#{@birthday.inspect} is not a date"
|
11
|
+
end
|
8
12
|
end
|
9
13
|
|
10
|
-
def age
|
11
|
-
|
12
|
-
(today.strftime('%Y%m%d').to_i - @birthday.strftime('%Y%m%d').to_i) / 10000
|
14
|
+
def age(asof: nil)
|
15
|
+
((asof || today).strftime('%Y%m%d').to_i - @birthday.strftime('%Y%m%d').to_i) / 10000
|
13
16
|
end
|
14
17
|
|
15
18
|
def today
|
@@ -3,8 +3,7 @@ class AgeCalculator
|
|
3
3
|
initializer 'age_calculator.active_model' do |app|
|
4
4
|
require 'age_calculator/validator'
|
5
5
|
|
6
|
-
|
7
|
-
I18n.load_path += locale_path unless I18n.load_path.include?(locale_path)
|
6
|
+
I18n.load_path += Pathname.new(__FILE__).dirname.parent.parent.glob('config/locales/*.yml')
|
8
7
|
end
|
9
8
|
end
|
10
9
|
end
|
@@ -5,7 +5,7 @@ class AgeValidator < ActiveModel::EachValidator
|
|
5
5
|
return record.errors.add(attribute, :blank, options) if value.blank?
|
6
6
|
return record.errors.add(attribute, :not_date, options) unless value.is_a?(Date)
|
7
7
|
|
8
|
-
age = AgeCalculator.new(value).age
|
8
|
+
age = AgeCalculator.new(value).age(asof: options[:asof])
|
9
9
|
|
10
10
|
options.slice(*COMPARATORS.keys).each do |option_key, option_value|
|
11
11
|
unless age.send(COMPARATORS[option_key], option_value.to_i)
|
data/test/age_calculator_test.rb
CHANGED
@@ -2,15 +2,28 @@ require 'test_helper'
|
|
2
2
|
|
3
3
|
describe AgeCalculator do
|
4
4
|
it 'calculates age from birthday' do
|
5
|
-
travel_to Date.new(
|
6
|
-
ac = AgeCalculator.new Date.new(
|
5
|
+
travel_to Date.new(2018,1,2) do
|
6
|
+
ac = AgeCalculator.new Date.new(2000,1,1)
|
7
7
|
ac.age.must_equal 18
|
8
8
|
|
9
|
-
ac = AgeCalculator.new Date.new(
|
9
|
+
ac = AgeCalculator.new Date.new(2000,1,2)
|
10
10
|
ac.age.must_equal 18
|
11
11
|
|
12
|
-
ac = AgeCalculator.new Date.new(
|
12
|
+
ac = AgeCalculator.new Date.new(2000,1,3)
|
13
13
|
ac.age.must_equal 17
|
14
14
|
end
|
15
15
|
end
|
16
|
+
|
17
|
+
it 'calculates age based on a given date' do
|
18
|
+
ac = AgeCalculator.new Date.new(2000,1,2)
|
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
|
23
|
+
end
|
24
|
+
|
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/)
|
28
|
+
end
|
16
29
|
end
|
data/test/age_validator_test.rb
CHANGED
@@ -6,24 +6,21 @@ describe AgeValidator do
|
|
6
6
|
end
|
7
7
|
|
8
8
|
it 'validates age' do
|
9
|
-
travel_to Date.new(
|
10
|
-
@adult.birthday = Date.new(
|
11
|
-
@adult.age.must_equal 18
|
9
|
+
travel_to Date.new(2018,1,2) do
|
10
|
+
@adult.birthday = Date.new(2000,1,1)
|
12
11
|
@adult.valid?.must_equal true
|
13
12
|
|
14
|
-
@adult.birthday = Date.new(
|
15
|
-
@adult.age.must_equal 18
|
13
|
+
@adult.birthday = Date.new(2000,1,2)
|
16
14
|
@adult.valid?.must_equal true
|
17
15
|
|
18
|
-
@adult.birthday = Date.new(
|
19
|
-
@adult.age.must_equal 17
|
16
|
+
@adult.birthday = Date.new(2000,1,3)
|
20
17
|
@adult.valid?.must_equal false
|
21
18
|
@adult.errors.full_messages.first.must_equal 'Birthday must be over 18'
|
22
19
|
end
|
23
20
|
end
|
24
21
|
|
25
22
|
it 'validates presence' do
|
26
|
-
travel_to Date.new(
|
23
|
+
travel_to Date.new(2018,1,2) do
|
27
24
|
@adult.birthday = nil
|
28
25
|
@adult.valid?.must_equal false
|
29
26
|
@adult.errors.full_messages.first.must_equal "Birthday can't be blank"
|
@@ -31,7 +28,7 @@ describe AgeValidator do
|
|
31
28
|
end
|
32
29
|
|
33
30
|
it 'validates date' do
|
34
|
-
travel_to Date.new(
|
31
|
+
travel_to Date.new(2018,1,2) do
|
35
32
|
@adult.birthday = 'invalid'
|
36
33
|
@adult.valid?.must_equal false
|
37
34
|
@adult.errors.full_messages.first.must_equal 'Birthday is not a valid date'
|
@@ -41,9 +38,22 @@ describe AgeValidator do
|
|
41
38
|
it 'allows blank' do
|
42
39
|
adult = AdultAllowBlank.new
|
43
40
|
|
44
|
-
travel_to Date.new(
|
41
|
+
travel_to Date.new(2018,1,2) do
|
45
42
|
adult.birthday = nil
|
46
43
|
adult.valid?.must_equal true
|
47
44
|
end
|
48
45
|
end
|
46
|
+
|
47
|
+
it 'validates age as of 2018-01-02' do
|
48
|
+
adult = AdultAsOf.new
|
49
|
+
|
50
|
+
adult.birthday = Date.new(2000,1,1)
|
51
|
+
adult.valid?.must_equal true
|
52
|
+
|
53
|
+
adult.birthday = Date.new(2000,1,2)
|
54
|
+
adult.valid?.must_equal true
|
55
|
+
|
56
|
+
adult.birthday = Date.new(2000,1,3)
|
57
|
+
adult.valid?.must_equal false
|
58
|
+
end
|
49
59
|
end
|
data/test/load_models.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'active_model'
|
2
|
+
require 'age_calculator/validator'
|
3
|
+
|
4
|
+
# Set timezone
|
5
|
+
require 'active_support/time' # For testing Date and TimeWithZone objects
|
6
|
+
Time.zone = 'Hawaii'
|
7
|
+
|
8
|
+
# Enable travel_to
|
9
|
+
require 'active_support/testing/time_helpers'
|
10
|
+
include ActiveSupport::Testing::TimeHelpers
|
11
|
+
|
12
|
+
# Locale
|
13
|
+
I18n.enforce_available_locales = true
|
14
|
+
I18n.load_path += Pathname.new(__FILE__).dirname.parent.glob('config/locales/*.yml')
|
15
|
+
|
16
|
+
class Adult
|
17
|
+
include ActiveModel::Validations
|
18
|
+
attr_accessor :birthday
|
19
|
+
validates :birthday, age: { over: 18 }
|
20
|
+
|
21
|
+
def age(asof: nil)
|
22
|
+
AgeCalculator.new(birthday).age(asof: asof)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
class AdultAllowBlank
|
27
|
+
include ActiveModel::Validations
|
28
|
+
attr_accessor :birthday
|
29
|
+
validates :birthday, age: { over: 18 }, allow_blank: true
|
30
|
+
end
|
31
|
+
|
32
|
+
class AdultAsOf
|
33
|
+
include ActiveModel::Validations
|
34
|
+
attr_accessor :birthday
|
35
|
+
validates :birthday, age: { over: 18, asof: Date.new(2018,1,2) }
|
36
|
+
end
|
data/test/test_helper.rb
CHANGED
@@ -1,37 +1,6 @@
|
|
1
1
|
require 'minitest/autorun'
|
2
|
+
require 'minitest/pride'
|
2
3
|
require 'minitest/spec'
|
3
|
-
require 'active_model'
|
4
4
|
require 'age_calculator'
|
5
|
-
require 'age_calculator/validator'
|
6
5
|
|
7
|
-
|
8
|
-
require 'active_support/time' # For testing Date and TimeWithZone objects
|
9
|
-
Time.zone = 'Hawaii'
|
10
|
-
|
11
|
-
# Enable travel_to
|
12
|
-
require 'active_support/testing/time_helpers'
|
13
|
-
include ActiveSupport::Testing::TimeHelpers
|
14
|
-
|
15
|
-
# Locale
|
16
|
-
I18n.enforce_available_locales = true
|
17
|
-
I18n.load_path += Dir.glob(File.expand_path('../../config/locales/*.yml', __FILE__))
|
18
|
-
|
19
|
-
class Adult
|
20
|
-
include ActiveModel::Validations
|
21
|
-
attr_accessor :birthday
|
22
|
-
validates :birthday, age: { over: 18 }
|
23
|
-
|
24
|
-
def age
|
25
|
-
AgeCalculator.new(birthday).age
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
class AdultAllowBlank
|
30
|
-
include ActiveModel::Validations
|
31
|
-
attr_accessor :birthday
|
32
|
-
validates :birthday, age: { over: 18 }, allow_blank: true
|
33
|
-
|
34
|
-
def age
|
35
|
-
AgeCalculator.new(birthday).age
|
36
|
-
end
|
37
|
-
end
|
6
|
+
require 'load_models'
|
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.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kenn Ejima
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-04-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -65,6 +65,7 @@ files:
|
|
65
65
|
- README.md
|
66
66
|
- Rakefile
|
67
67
|
- age_calculator.gemspec
|
68
|
+
- bin/console
|
68
69
|
- config/locales/en.yml
|
69
70
|
- lib/age_calculator.rb
|
70
71
|
- lib/age_calculator/railtie.rb
|
@@ -72,6 +73,7 @@ files:
|
|
72
73
|
- lib/age_calculator/version.rb
|
73
74
|
- test/age_calculator_test.rb
|
74
75
|
- test/age_validator_test.rb
|
76
|
+
- test/load_models.rb
|
75
77
|
- test/test_helper.rb
|
76
78
|
homepage: https://github.com/kenn/age_calculator
|
77
79
|
licenses:
|
@@ -93,11 +95,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
93
95
|
version: '0'
|
94
96
|
requirements: []
|
95
97
|
rubyforge_project:
|
96
|
-
rubygems_version: 2.
|
98
|
+
rubygems_version: 2.7.6
|
97
99
|
signing_key:
|
98
100
|
specification_version: 4
|
99
101
|
summary: A simple age calculator that just works
|
100
102
|
test_files:
|
101
103
|
- test/age_calculator_test.rb
|
102
104
|
- test/age_validator_test.rb
|
105
|
+
- test/load_models.rb
|
103
106
|
- test/test_helper.rb
|