Class: PeriodsValidator::Validator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/periods_validator/validator.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Validator

Returns a new instance of Validator.



5
6
7
8
9
10
11
# File 'lib/periods_validator/validator.rb', line 5

def initialize(options)
  @end_range = options.fetch(:end_range, nil)
  @end_range_attribute = options.fetch(:end_range_attribute, :end_range)
  @periods = options.fetch(:in, [])

  super
end

Instance Method Details

#validate_each(record, _attribute, value) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/periods_validator/validator.rb', line 13

def validate_each(record, _attribute, value)
  end_range = @end_range || record.send(@end_range_attribute)
  options = {
    start_range: value,
    end_range: end_range,
    periods: @periods
  }

  validator = PeriodsValidator::Periods.new(options)

  return if validator.valid?

  record.errors.add(:base, validator.error)
end