Class: ActiveDateRange::Validators::DateRangeValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Includes:
ResolveValue
Defined in:
lib/active_date_range/validators/date_range_validator.rb

Constant Summary collapse

DURATION_OPTIONS =
%i[minimum_duration maximum_duration exact_duration duration].freeze
GRANULARITY_MAP =
{
  month: :full_month?,
  quarter: :full_quarter?,
  year: :full_year?,
  week: :full_week?
}.freeze
START_ALIGNMENT_MAP =
{
  beginning_of_month: :begin_at_beginning_of_month?,
  beginning_of_quarter: :begin_at_beginning_of_quarter?,
  beginning_of_year: :begin_at_beginning_of_year?,
  beginning_of_week: :begin_at_beginning_of_week?
}.freeze
END_ALIGNMENT_MAP =
{
  end_of_month: ->(r) { r.end == r.end.at_end_of_month },
  end_of_quarter: ->(r) { r.end == r.end.at_end_of_quarter },
  end_of_year: ->(r) { r.end == r.end.at_end_of_year },
  end_of_week: ->(r) { r.end == r.end.at_end_of_week }
}.freeze

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/active_date_range/validators/date_range_validator.rb', line 28

def validate_each(record, attribute, value)
  unless value.is_a?(ActiveDateRange::DateRange)
    record.errors.add(attribute, :not_a_date_range, **options.except(*known_options))
    return
  end

  validate_bounded(record, attribute, value)
  validate_duration(record, attribute, value)
  validate_full_periods_of(record, attribute, value)
  validate_starts_on(record, attribute, value)
  validate_ends_on(record, attribute, value)
  validate_covers(record, attribute, value)
end