Class: CvrValidator

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

Constant Summary collapse

MOD_11_WEIGHTS =
[2, 7, 6, 5, 4, 3, 2].freeze

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



4
5
6
7
8
9
10
11
12
13
# File 'lib/cvr_validator.rb', line 4

def validate_each(record, attribute, value)
  return record.errors.add(attribute, :blank) if value.blank?

  digits = convert_to_digits(value)

  record.errors.add(attribute, :too_short) if digits.length < 8
  record.errors.add(attribute, :too_long) if digits.length > 8
  record.errors.add(attribute, :bad_format) unless value.match?(/\A[\s\d-]+\z/)
  record.errors.add(attribute, :invalid_cvr_number) unless valid_check_digit?(digits)
end