birth_date_validator 0.2 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/birth_date_validator/validator.rb +18 -14
- data/lib/birth_date_validator/version.rb +1 -1
- data/spec/birth_date_validator_spec.rb +1 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 80c6c29bc44619078a85c24f6d9ea698d8f8edb0
|
4
|
+
data.tar.gz: 46b4456ba2003549d880c6905174d8d4e9735efd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
13
|
+
record.errors.add(attribute, options.fetch(:message, :invalid)) unless BirthDateValidator.valid?(value, options)
|
14
|
+
end
|
14
15
|
|
15
|
-
|
16
|
-
|
17
|
-
|
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
|
-
|
24
|
-
|
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
|
-
|
29
|
-
|
30
|
-
|
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
|