Class: BirthDateValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/birth_date_validator/version.rb,
lib/birth_date_validator/validator.rb

Overview

          DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
                  Version 2, December 2004

          DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
 TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION

0. You just DO WHAT THE FUCK YOU WANT TO.

++

Constant Summary collapse

VERSION =
'0.2.3'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.valid?(birth_date, options) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/birth_date_validator/validator.rb', line 17

def valid?(birth_date, options)
  age_to_validate = age(birth_date)

  if options[:at_least].present?
    age_to_validate >= age(options[:at_least])
  elsif options[:less_then].present?
    age_to_validate < age(options[:less_then])
  elsif options[:range].present?
    age_to_validate >= age(options[:range].first) && age_to_validate <= age(options[:range].last)
  end
rescue
  false
end

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



12
13
14
# File 'lib/birth_date_validator/validator.rb', line 12

def validate_each(record, attribute, value)
  record.errors.add(attribute, options.fetch(:message, :invalid)) unless BirthDateValidator.valid?(value, options)
end