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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: bada8221cdeadfc09ed63a8f275efcc302b3084e
4
- data.tar.gz: 9c3fb7c34164a261153b64723a2ce4e713842bd2
2
+ SHA256:
3
+ metadata.gz: aee3d23e9bb225c4c03eb269c6a14da8c0f02c2bc8f0b389e0df617523f468d7
4
+ data.tar.gz: f5efaf19f902905a43cac4d84ffb3b3f2b69a4fa2d09a09fe3079041d3a7d399
5
5
  SHA512:
6
- metadata.gz: a2ffd2f24b305147d379716ab71e216f8ba0d0910b9054ca83d95759499f51518f456d437a2e65d5959fd1808bedf71b9789f52be03f8ca3a65c553eedd4f8eb
7
- data.tar.gz: a3ad1f02314fdc79975b2a059c171e818e4b29a1cda9ff5380e27661cc75371d322e12fc59a7b39201c00bc0ba68948ffff3a843b30a196495b91735de8074f7
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, 2014 and calculate the age of people whose birthdays are May 15, 1996 (should be 18 years old) and May 17, 1996 (should be 17 years old) respectively.
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
- > 20140516 - 19960517
18
+ > 20180516 - 20000517
19
19
  => 179999
20
20
  > 179999 / 10000
21
21
  => 17
22
- > 20140516 - 19960515
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(1987,12,31)
51
+ birthday = Date.new(2000,1,1)
52
+ ac = AgeCalculator.new(birthday)
53
+
54
+ ac.age
55
+ => 18
50
56
 
51
- AgeCalculator.new(birthday).age
52
- => 26
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
 
@@ -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{^bin/}) { |f| File.basename(f) }
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
 
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'age_calculator'
5
+
6
+ require_relative '../test/load_models'
7
+
8
+ require 'irb'
9
+ IRB.start
@@ -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
- return unless @birthday
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
- locale_path = Dir.glob(File.expand_path('../../../config/locales/*.yml', __FILE__))
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)
@@ -1,3 +1,3 @@
1
1
  class AgeCalculator
2
- VERSION = '1.1.0'
2
+ VERSION = '1.2.0'
3
3
  end
@@ -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(2014,1,2) do
6
- ac = AgeCalculator.new Date.new(1996,1,1)
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(1996,1,2)
9
+ ac = AgeCalculator.new Date.new(2000,1,2)
10
10
  ac.age.must_equal 18
11
11
 
12
- ac = AgeCalculator.new Date.new(1996,1,3)
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
@@ -6,24 +6,21 @@ describe AgeValidator do
6
6
  end
7
7
 
8
8
  it 'validates age' do
9
- travel_to Date.new(2014,1,2) do
10
- @adult.birthday = Date.new(1996,1,1)
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(1996,1,2)
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(1996,1,3)
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(2014,1,2) do
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(2014,1,2) do
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(2014,1,2) do
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
@@ -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
@@ -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
- # Set timezone
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.1.0
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: 2014-11-18 00:00:00.000000000 Z
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.2.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