birth_date_validator 0.2 → 0.2.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cdf56f43fbfe67dd19bc0acaf83e153d68d3e0e9
4
- data.tar.gz: 889a7bb058a1fad32cc194a8cc960ce0de10e260
3
+ metadata.gz: 80c6c29bc44619078a85c24f6d9ea698d8f8edb0
4
+ data.tar.gz: 46b4456ba2003549d880c6905174d8d4e9735efd
5
5
  SHA512:
6
- metadata.gz: 401052166287b650a981d935c56d9be68d8747ce92c387bab52885790fd93d28556523eb961a775ac254153bb82985428923a66e27b5199bd09f6a3a6c165bec
7
- data.tar.gz: 92d01741c4825da57e3385572570dec00a681de5e02d4c52ea5ceb85ac362fd32d1a5a3a5c5583437c8123723efa168dcca963b31ca857e471dd99a2c9a61680
6
+ metadata.gz: 5dabceae615e23dc25f58186d23cc8b7ac2d9192b711a38e520c5367fcf5c81264148f9eec7cb8625e441d840ff373e4d9d8a709f6ec6b3b0005ab30cdb89722
7
+ data.tar.gz: 445646a4255093093dd5fce53aa79cee4ec22d1081cac65f82493b8f6a3ca15c1fed5e651a6de2875dd8a2c751940e1a7de227da09e0218c0a0cb7a4183a61de
@@ -10,23 +10,27 @@
10
10
 
11
11
  class BirthDateValidator < ActiveModel::EachValidator
12
12
  def validate_each(record, attribute, value)
13
- age_to_validate = age(value)
13
+ record.errors.add(attribute, options.fetch(:message, :invalid)) unless BirthDateValidator.valid?(value, options)
14
+ end
14
15
 
15
- invalid = if options[:at_least].present?
16
- age_to_validate < age(options[:at_least])
17
- elsif options[:less_then].present?
18
- age_to_validate >= age(options[:less_then])
19
- elsif options[:range].present?
20
- age_to_validate < age(options[:range].first) || age_to_validate > age(options[:range].last)
21
- end
16
+ class << self
17
+ def valid?(birth_date, options)
18
+ age_to_validate = age(birth_date)
22
19
 
23
- record.errors.add(attribute, options.fetch(:message, :invalid)) if invalid
24
- end
20
+ if options[:at_least].present?
21
+ age_to_validate >= age(options[:at_least])
22
+ elsif options[:less_then].present?
23
+ age_to_validate < age(options[:less_then])
24
+ elsif options[:range].present?
25
+ age_to_validate >= age(options[:range].first) && age_to_validate <= age(options[:range].last)
26
+ end
27
+ end
25
28
 
26
- private
29
+ private
27
30
 
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
+ def age(birth_date)
32
+ now = Date.today
33
+ now.year - birth_date.year - ((now.month > birth_date.month || (now.month == birth_date.month && now.day >= birth_date.day)) ? 0 : 1)
34
+ end
31
35
  end
32
36
  end
@@ -9,5 +9,5 @@
9
9
  #++
10
10
 
11
11
  class BirthDateValidator
12
- VERSION = '0.2'
12
+ VERSION = '0.2.1'
13
13
  end
@@ -5,6 +5,7 @@ describe BirthDateValidator do
5
5
  context '18 years old' do
6
6
  it 'is valid' do
7
7
  expect(TestUserOverAge.new(birth_date: 18.years.ago)).to be_valid
8
+ expect(BirthDateValidator.valid?(18.years.ago, at_least: 18.years.ago)).to be_truthy
8
9
  end
9
10
  end
10
11
 
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.2'
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Giovanni Capuano