birth_date_validator 0.1.1 → 0.2

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: 637fbf5ffb1b89666920f75b64b28235675e6e05
4
- data.tar.gz: ea0ae65a07369d83b7afb1a98a7bf2cd756f8f3b
3
+ metadata.gz: cdf56f43fbfe67dd19bc0acaf83e153d68d3e0e9
4
+ data.tar.gz: 889a7bb058a1fad32cc194a8cc960ce0de10e260
5
5
  SHA512:
6
- metadata.gz: 6abb63db105fd429c30576ffee9576715f8c62305b67b8711cc33a90403239221a546704e510cd9c7316461089d861d7fa739e78780407f322e75c0c81b3db79
7
- data.tar.gz: 8239879492d557669c5186c78a7776d6384b1de59017019209b69e6b7a964f50a75fb413ba5748c58c2f5397955e991d4f5c05fefed70f882bc5a8a6f842e2b3
6
+ metadata.gz: 401052166287b650a981d935c56d9be68d8747ce92c387bab52885790fd93d28556523eb961a775ac254153bb82985428923a66e27b5199bd09f6a3a6c165bec
7
+ data.tar.gz: 92d01741c4825da57e3385572570dec00a681de5e02d4c52ea5ceb85ac362fd32d1a5a3a5c5583437c8123723efa168dcca963b31ca857e471dd99a2c9a61680
@@ -10,14 +10,23 @@
10
10
 
11
11
  class BirthDateValidator < ActiveModel::EachValidator
12
12
  def validate_each(record, attribute, value)
13
+ age_to_validate = age(value)
14
+
13
15
  invalid = if options[:at_least].present?
14
- value.year > options[:at_least].year
16
+ age_to_validate < age(options[:at_least])
15
17
  elsif options[:less_then].present?
16
- value.year <= options[:less_then].year
18
+ age_to_validate >= age(options[:less_then])
17
19
  elsif options[:range].present?
18
- value.year > options[:range].first.year || value.year < options[:range].last.year
20
+ age_to_validate < age(options[:range].first) || age_to_validate > age(options[:range].last)
19
21
  end
20
22
 
21
23
  record.errors.add(attribute, options.fetch(:message, :invalid)) if invalid
22
24
  end
25
+
26
+ private
27
+
28
+ def age(birth_date)
29
+ now = Date.today
30
+ now.year - birth_date.year - ((now.month > birth_date.month || (now.month == birth_date.month && now.day >= birth_date.day)) ? 0 : 1)
31
+ end
23
32
  end
@@ -9,5 +9,5 @@
9
9
  #++
10
10
 
11
11
  class BirthDateValidator
12
- VERSION = '0.1.1'
12
+ VERSION = '0.2'
13
13
  end
@@ -2,6 +2,12 @@ require './spec_helper'
2
2
 
3
3
  describe BirthDateValidator do
4
4
  describe 'user has to have at least 18 years' do
5
+ context '18 years old' do
6
+ it 'is valid' do
7
+ expect(TestUserOverAge.new(birth_date: 18.years.ago)).to be_valid
8
+ end
9
+ end
10
+
5
11
  context '19 years old' do
6
12
  it 'is valid' do
7
13
  expect(TestUserOverAge.new(birth_date: 19.years.ago)).to be_valid
@@ -17,27 +23,21 @@ describe BirthDateValidator do
17
23
  expect(subject.errors[:birth_date][0]).to include 'older'
18
24
  end
19
25
  end
26
+ end
20
27
 
21
- context '18 years old' do
28
+ describe 'user has to have less then 18 years' do
29
+ context '17 years old' do
22
30
  it 'is valid' do
23
- expect(TestUserOverAge.new(birth_date: 18.years.ago)).to be_valid
31
+ expect(TestUserUnderAge.new(birth_date: 17.years.ago)).to be_valid
24
32
  end
25
33
  end
26
- end
27
34
 
28
- describe 'user has to have less then 18 years' do
29
35
  context '19 years old' do
30
36
  it 'is not valid' do
31
37
  expect(TestUserUnderAge.new(birth_date: 19.years.ago)).to_not be_valid
32
38
  end
33
39
  end
34
40
 
35
- context '17 years old' do
36
- it 'is valid' do
37
- expect(TestUserUnderAge.new(birth_date: 17.years.ago)).to be_valid
38
- end
39
- end
40
-
41
41
  context '18 years old' do
42
42
  it 'is not valid' do
43
43
  expect(TestUserUnderAge.new(birth_date: 18.years.ago)).to_not be_valid
@@ -46,18 +46,18 @@ describe BirthDateValidator do
46
46
  end
47
47
 
48
48
  describe 'user has to have from 18 and 30 years' do
49
- context '17 years old' do
50
- it 'is not valid' do
51
- expect(TestUserRange.new(birth_date: 17.years.ago)).to_not be_valid
52
- end
53
- end
54
-
55
49
  context '30 years old' do
56
50
  it 'is valid' do
57
51
  expect(TestUserRange.new(birth_date: 30.years.ago)).to be_valid
58
52
  end
59
53
  end
60
54
 
55
+ context '17 years old' do
56
+ it 'is not valid' do
57
+ expect(TestUserRange.new(birth_date: 17.years.ago)).to_not be_valid
58
+ end
59
+ end
60
+
61
61
  context '31 years old' do
62
62
  it 'is not valid' do
63
63
  expect(TestUserRange.new(birth_date: 31.years.ago)).to_not be_valid
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: birth_date_validator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: '0.2'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Giovanni Capuano
@@ -66,8 +66,8 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
- description: Set through various parameters the minimum or the range of ages in which
70
- your users have to be.
69
+ description: Minimal birth date validator to check if the user has the right age to
70
+ sign in the website.
71
71
  email: webmaster@giovannicapuano.net
72
72
  executables: []
73
73
  extensions: []