class DailyAffirmation::Validators::DateValidator

This affirmation ensures an attribute is a date and if a :before or :after option is given, that the attribute is before or after the selected date.

@option opts [Date] :before if present checked to ensure value is before

provided date.

@option opts [Date] :after if present checked to ensure value is after

provided date.

Public Instance Methods

error_message() click to toggle source
# File lib/daily_affirmation/validators/date_validator.rb, line 23
def error_message
  @error_message ||= i18n_error_message(
    :format, :default => "#{attribute} is not a valid #{as}"
  )
end
valid?() click to toggle source
# File lib/daily_affirmation/validators/date_validator.rb, line 19
def valid?
  @valid ||= parseable? && before? && after?
end

Private Instance Methods

after() click to toggle source
# File lib/daily_affirmation/validators/date_validator.rb, line 52
def after
  @after ||= -> {
    val = opts.fetch(:after, NullDateLike.new)
    val.respond_to?(:call) ? val.call : val
  }.call
end
after?() click to toggle source
# File lib/daily_affirmation/validators/date_validator.rb, line 59
def after?
  after < value
end
as() click to toggle source
# File lib/daily_affirmation/validators/date_validator.rb, line 37
def as
  opts.fetch(:as, :date)
end
before() click to toggle source
# File lib/daily_affirmation/validators/date_validator.rb, line 41
def before
  @before ||= -> {
    val = opts.fetch(:before, NullDateLike.new)
    val.respond_to?(:call) ? val.call : val
  }.call
end
before?() click to toggle source
# File lib/daily_affirmation/validators/date_validator.rb, line 48
def before?
  before > value
end
klass() click to toggle source
# File lib/daily_affirmation/validators/date_validator.rb, line 63
def klass
  case as
  when :date
    Date
  when :datetime
    DateTime
  when :time
    Time
  else
    raise OptionError,
      "Invalid value for `:as`: #{opts[:as]}. Valid options are :date, :datetime and :time."
  end
end
parseable?() click to toggle source
# File lib/daily_affirmation/validators/date_validator.rb, line 31
def parseable?
  value.is_a?(klass) || !!klass.parse(value)
rescue ArgumentError
  false
end