age_calculator 1.0.0 → 1.1.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 +4 -4
- data/config/locales/en.yml +1 -1
- data/lib/age_calculator/validator.rb +2 -3
- data/lib/age_calculator/version.rb +1 -1
- data/test/age_validator_test.rb +19 -2
- data/test/test_helper.rb +10 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bada8221cdeadfc09ed63a8f275efcc302b3084e
|
4
|
+
data.tar.gz: 9c3fb7c34164a261153b64723a2ce4e713842bd2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a2ffd2f24b305147d379716ab71e216f8ba0d0910b9054ca83d95759499f51518f456d437a2e65d5959fd1808bedf71b9789f52be03f8ca3a65c553eedd4f8eb
|
7
|
+
data.tar.gz: a3ad1f02314fdc79975b2a059c171e818e4b29a1cda9ff5380e27661cc75371d322e12fc59a7b39201c00bc0ba68948ffff3a843b30a196495b91735de8074f7
|
data/README.md
CHANGED
@@ -4,7 +4,7 @@ A simple age calculator for Ruby. Comes with age validator for ActiveModel.
|
|
4
4
|
|
5
5
|
# Background
|
6
6
|
|
7
|
-
You might think calculating age from a birthday is easy, but you'd be wrong - there is [so much
|
7
|
+
You might think calculating age from a birthday is easy, but you'd be wrong - there is [so](https://www.ruby-forum.com/topic/49265) [much](http://stackoverflow.com/questions/10463400/age-calculation-in-ruby) [confusion](http://stackoverflow.com/questions/819263/get-persons-age-in-ruby) on the topic, and sadly, many people are doing it wrong. Even right ways can look really complicated.
|
8
8
|
|
9
9
|
Here's the simplest, cleanest way:
|
10
10
|
|
@@ -46,7 +46,10 @@ $ gem install age_calculator
|
|
46
46
|
For a basic usage:
|
47
47
|
|
48
48
|
```ruby
|
49
|
+
birthday = Date.new(1987,12,31)
|
50
|
+
|
49
51
|
AgeCalculator.new(birthday).age
|
52
|
+
=> 26
|
50
53
|
```
|
51
54
|
|
52
55
|
For a model with a validation on the age:
|
@@ -55,9 +58,6 @@ For a model with a validation on the age:
|
|
55
58
|
class Adult < ActiveRecord::Base
|
56
59
|
validates :birthday, age: { over: 18 }
|
57
60
|
|
58
|
-
def age
|
59
|
-
AgeCalculator.new(birthday).age
|
60
|
-
end
|
61
61
|
end
|
62
62
|
```
|
63
63
|
|
data/config/locales/en.yml
CHANGED
@@ -2,9 +2,8 @@ class AgeValidator < ActiveModel::EachValidator
|
|
2
2
|
COMPARATORS = { :over => :>=, :under => :<= }.freeze
|
3
3
|
|
4
4
|
def validate_each(record, attribute, value)
|
5
|
-
|
6
|
-
|
7
|
-
end
|
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)
|
8
7
|
|
9
8
|
age = AgeCalculator.new(value).age
|
10
9
|
|
data/test/age_validator_test.rb
CHANGED
@@ -22,11 +22,28 @@ describe AgeValidator do
|
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
25
|
-
it '
|
25
|
+
it 'validates presence' do
|
26
26
|
travel_to Date.new(2014,1,2) do
|
27
27
|
@adult.birthday = nil
|
28
28
|
@adult.valid?.must_equal false
|
29
|
-
@adult.errors.full_messages.first.must_equal
|
29
|
+
@adult.errors.full_messages.first.must_equal "Birthday can't be blank"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'validates date' do
|
34
|
+
travel_to Date.new(2014,1,2) do
|
35
|
+
@adult.birthday = 'invalid'
|
36
|
+
@adult.valid?.must_equal false
|
37
|
+
@adult.errors.full_messages.first.must_equal 'Birthday is not a valid date'
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'allows blank' do
|
42
|
+
adult = AdultAllowBlank.new
|
43
|
+
|
44
|
+
travel_to Date.new(2014,1,2) do
|
45
|
+
adult.birthday = nil
|
46
|
+
adult.valid?.must_equal true
|
30
47
|
end
|
31
48
|
end
|
32
49
|
end
|
data/test/test_helper.rb
CHANGED
@@ -25,3 +25,13 @@ class Adult
|
|
25
25
|
AgeCalculator.new(birthday).age
|
26
26
|
end
|
27
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
|
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.1.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
|
+
date: 2014-11-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|