age_calculator 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +22 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +70 -0
- data/Rakefile +9 -0
- data/age_calculator.gemspec +24 -0
- data/config/locales/en.yml +6 -0
- data/lib/age_calculator.rb +18 -0
- data/lib/age_calculator/railtie.rb +10 -0
- data/lib/age_calculator/validator.rb +17 -0
- data/lib/age_calculator/version.rb +3 -0
- data/test/age_calculator_test.rb +16 -0
- data/test/age_validator_test.rb +32 -0
- data/test/test_helper.rb +27 -0
- metadata +103 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 7308d45111a9fa22ef7e2b79c19aadd8e9d94832
|
4
|
+
data.tar.gz: 17e90eff2afa3da30ee7216f1ea552229460ccb7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 30881c149b2ded17a805de66e48a684a039aa9c5114340643f2a80b4c70ad3434eb33ade2254776e4ece14702fe76ad6e42309fa8dabd686d8cc0f38afccc285
|
7
|
+
data.tar.gz: d5f82b72296c4f199a30fea1d8776e0e8e8e833893c36a59dedad1f15f49b7fecd3ff213539f089d5218ae783e9cff753916413bf9d91161624663eed4ca22c8
|
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Kenn Ejima
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
# AgeCalculator
|
2
|
+
|
3
|
+
A simple age calculator for Ruby. Comes with age validator for ActiveModel.
|
4
|
+
|
5
|
+
# Background
|
6
|
+
|
7
|
+
You might think calculating age from a birthday is easy, but you'd be wrong - there is [so much confusion](http://stackoverflow.com/questions/10463400/age-calculation-in-ruby) on the topic, and sadly, many people are doing it wrong. Even right ways can look really complicated.
|
8
|
+
|
9
|
+
Here's the simplest, cleanest way:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
(today.to_s(:number).to_i - birthday.to_s(:number).to_i) / 10000
|
13
|
+
```
|
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.
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
> 20140516 - 19960517
|
19
|
+
=> 179999
|
20
|
+
> 179999 / 10000
|
21
|
+
=> 17
|
22
|
+
> 20140516 - 19960515
|
23
|
+
=> 180001
|
24
|
+
> 180001 / 10000
|
25
|
+
=> 18
|
26
|
+
```
|
27
|
+
|
28
|
+
Couldn't be simpler. Note that Ruby truncates the decimal fraction.
|
29
|
+
|
30
|
+
## Installation
|
31
|
+
|
32
|
+
Add this line to your application's Gemfile:
|
33
|
+
|
34
|
+
```
|
35
|
+
gem 'age_calculator'
|
36
|
+
```
|
37
|
+
|
38
|
+
Or install it yourself as:
|
39
|
+
|
40
|
+
```
|
41
|
+
$ gem install age_calculator
|
42
|
+
```
|
43
|
+
|
44
|
+
## Usage
|
45
|
+
|
46
|
+
For a basic usage:
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
AgeCalculator.new(birthday).age
|
50
|
+
```
|
51
|
+
|
52
|
+
For a model with a validation on the age:
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
class Adult < ActiveRecord::Base
|
56
|
+
validates :birthday, age: { over: 18 }
|
57
|
+
|
58
|
+
def age
|
59
|
+
AgeCalculator.new(birthday).age
|
60
|
+
end
|
61
|
+
end
|
62
|
+
```
|
63
|
+
|
64
|
+
## Contributing
|
65
|
+
|
66
|
+
1. Fork it ( https://github.com/[my-github-username]/age_calculator/fork )
|
67
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
68
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
69
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
70
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'age_calculator/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'age_calculator'
|
8
|
+
spec.version = AgeCalculator::VERSION
|
9
|
+
spec.authors = ['Kenn Ejima']
|
10
|
+
spec.email = ['kenn.ejima@gmail.com']
|
11
|
+
spec.summary = %q{A simple age calculator that just works}
|
12
|
+
spec.description = %q{A simple age calculator that just works}
|
13
|
+
spec.homepage = 'https://github.com/kenn/age_calculator'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
|
21
|
+
spec.add_development_dependency 'rake'
|
22
|
+
spec.add_development_dependency 'minitest'
|
23
|
+
spec.add_development_dependency 'activemodel', '>= 4.1' # ActiveSupport::Testing::TimeHelpers
|
24
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'date'
|
2
|
+
require 'age_calculator/version'
|
3
|
+
require 'age_calculator/railtie' if defined?(Rails)
|
4
|
+
|
5
|
+
class AgeCalculator
|
6
|
+
def initialize(birthday)
|
7
|
+
@birthday = birthday
|
8
|
+
end
|
9
|
+
|
10
|
+
def age
|
11
|
+
return unless @birthday
|
12
|
+
(today.strftime('%Y%m%d').to_i - @birthday.strftime('%Y%m%d').to_i) / 10000
|
13
|
+
end
|
14
|
+
|
15
|
+
def today
|
16
|
+
Time.respond_to?(:zone) ? Time.zone.today : Date.today
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
class AgeCalculator
|
2
|
+
class Railtie < Rails::Railtie
|
3
|
+
initializer 'age_calculator.active_model' do |app|
|
4
|
+
require 'age_calculator/validator'
|
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)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
class AgeValidator < ActiveModel::EachValidator
|
2
|
+
COMPARATORS = { :over => :>=, :under => :<= }.freeze
|
3
|
+
|
4
|
+
def validate_each(record, attribute, value)
|
5
|
+
unless value.is_a?(Date)
|
6
|
+
return record.errors.add(attribute, :not_date, options)
|
7
|
+
end
|
8
|
+
|
9
|
+
age = AgeCalculator.new(value).age
|
10
|
+
|
11
|
+
options.slice(*COMPARATORS.keys).each do |option_key, option_value|
|
12
|
+
unless age.send(COMPARATORS[option_key], option_value.to_i)
|
13
|
+
record.errors.add(attribute, :"age_#{option_key}", options.merge(age: option_value))
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
describe AgeCalculator do
|
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)
|
7
|
+
ac.age.must_equal 18
|
8
|
+
|
9
|
+
ac = AgeCalculator.new Date.new(1996,1,2)
|
10
|
+
ac.age.must_equal 18
|
11
|
+
|
12
|
+
ac = AgeCalculator.new Date.new(1996,1,3)
|
13
|
+
ac.age.must_equal 17
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
describe AgeValidator do
|
4
|
+
before do
|
5
|
+
@adult = Adult.new
|
6
|
+
end
|
7
|
+
|
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
|
12
|
+
@adult.valid?.must_equal true
|
13
|
+
|
14
|
+
@adult.birthday = Date.new(1996,1,2)
|
15
|
+
@adult.age.must_equal 18
|
16
|
+
@adult.valid?.must_equal true
|
17
|
+
|
18
|
+
@adult.birthday = Date.new(1996,1,3)
|
19
|
+
@adult.age.must_equal 17
|
20
|
+
@adult.valid?.must_equal false
|
21
|
+
@adult.errors.full_messages.first.must_equal 'Birthday must be over 18'
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'rejects invalid values' do
|
26
|
+
travel_to Date.new(2014,1,2) do
|
27
|
+
@adult.birthday = nil
|
28
|
+
@adult.valid?.must_equal false
|
29
|
+
@adult.errors.full_messages.first.must_equal 'Birthday is not a date'
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require 'minitest/spec'
|
3
|
+
require 'active_model'
|
4
|
+
require 'age_calculator'
|
5
|
+
require 'age_calculator/validator'
|
6
|
+
|
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
|
metadata
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: age_calculator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Kenn Ejima
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-05-17 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rake
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: minitest
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: activemodel
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '4.1'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '4.1'
|
55
|
+
description: A simple age calculator that just works
|
56
|
+
email:
|
57
|
+
- kenn.ejima@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- age_calculator.gemspec
|
68
|
+
- config/locales/en.yml
|
69
|
+
- lib/age_calculator.rb
|
70
|
+
- lib/age_calculator/railtie.rb
|
71
|
+
- lib/age_calculator/validator.rb
|
72
|
+
- lib/age_calculator/version.rb
|
73
|
+
- test/age_calculator_test.rb
|
74
|
+
- test/age_validator_test.rb
|
75
|
+
- test/test_helper.rb
|
76
|
+
homepage: https://github.com/kenn/age_calculator
|
77
|
+
licenses:
|
78
|
+
- MIT
|
79
|
+
metadata: {}
|
80
|
+
post_install_message:
|
81
|
+
rdoc_options: []
|
82
|
+
require_paths:
|
83
|
+
- lib
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
requirements: []
|
95
|
+
rubyforge_project:
|
96
|
+
rubygems_version: 2.2.2
|
97
|
+
signing_key:
|
98
|
+
specification_version: 4
|
99
|
+
summary: A simple age calculator that just works
|
100
|
+
test_files:
|
101
|
+
- test/age_calculator_test.rb
|
102
|
+
- test/age_validator_test.rb
|
103
|
+
- test/test_helper.rb
|