class BirthDateValidator

Constants

VERSION

Public Class Methods

valid?(birth_date, options) click to toggle source
# 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

Private Class Methods

age(birth_date) click to toggle source
# File lib/birth_date_validator/validator.rb, line 33
def age(birth_date)
  now = Date.today
  now.year - birth_date.year - ((now.month > birth_date.month || (now.month == birth_date.month && now.day >= birth_date.day)) ? 0 : 1)
end

Public Instance Methods

validate_each(record, attribute, value) click to toggle source
# 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