age_calculator 1.0.0 → 1.1.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
2
  SHA1:
3
- metadata.gz: 7308d45111a9fa22ef7e2b79c19aadd8e9d94832
4
- data.tar.gz: 17e90eff2afa3da30ee7216f1ea552229460ccb7
3
+ metadata.gz: bada8221cdeadfc09ed63a8f275efcc302b3084e
4
+ data.tar.gz: 9c3fb7c34164a261153b64723a2ce4e713842bd2
5
5
  SHA512:
6
- metadata.gz: 30881c149b2ded17a805de66e48a684a039aa9c5114340643f2a80b4c70ad3434eb33ade2254776e4ece14702fe76ad6e42309fa8dabd686d8cc0f38afccc285
7
- data.tar.gz: d5f82b72296c4f199a30fea1d8776e0e8e8e833893c36a59dedad1f15f49b7fecd3ff213539f089d5218ae783e9cff753916413bf9d91161624663eed4ca22c8
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 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.
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
 
@@ -1,6 +1,6 @@
1
1
  en:
2
2
  errors:
3
3
  messages:
4
- not_date: "is not a date"
4
+ not_date: "is not a valid date"
5
5
  age_over: "must be over %{age}"
6
6
  age_under: "must be under %{age}"
@@ -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
- unless value.is_a?(Date)
6
- return record.errors.add(attribute, :not_date, options)
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
 
@@ -1,3 +1,3 @@
1
1
  class AgeCalculator
2
- VERSION = '1.0.0'
2
+ VERSION = '1.1.0'
3
3
  end
@@ -22,11 +22,28 @@ describe AgeValidator do
22
22
  end
23
23
  end
24
24
 
25
- it 'rejects invalid values' do
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 'Birthday is not a date'
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.0.0
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-05-17 00:00:00.000000000 Z
11
+ date: 2014-11-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake