class SedolValidator

Public Instance Methods

validate_each(record, attribute, value) click to toggle source
# File lib/active_validation/validators/sedol_validator.rb, line 7
def validate_each(record, attribute, value)
  return if valid?(value.to_s)

  record.errors[attribute] <<
    (options[:message] || I18n.t('active_validation.errors.messages.sedol'))
end

Private Instance Methods

valid?(value) click to toggle source
# File lib/active_validation/validators/sedol_validator.rb, line 33
def valid?(value)
  valid_length?(value) &&
    valid_format?(value) &&
    valid_checksum?(value)
end
valid_checksum?(value) click to toggle source
# File lib/active_validation/validators/sedol_validator.rb, line 16
def valid_checksum?(value)
  digits = value.chars.map { |dgt| /[A-Z]/.match?(dgt) ? (dgt.ord - 55) : dgt.to_i }

  total = 0
  digits.each_with_index { |dgt, idx| total += (WEIGHTS[idx] * dgt) }

  (10 - total % 10) % 10
end
valid_format?(value) click to toggle source
# File lib/active_validation/validators/sedol_validator.rb, line 25
def valid_format?(value)
  value =~ /^([A-Z0-9]{6})(\d{1})$/
end
valid_length?(value) click to toggle source
# File lib/active_validation/validators/sedol_validator.rb, line 29
def valid_length?(value)
  value.present?
end