Sha256: f2778a0b9d2b3a10673f7985236eb12e44cd3684eb1ec0eb097cb90ba6ac8ab8
Contents?: true
Size: 779 Bytes
Versions: 8
Compression:
Stored size: 779 Bytes
Contents
class SedolValidator < ActiveModel::EachValidator WEIGHTS ||= [1, 3, 1, 7, 3, 9, 1].freeze 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 def valid_checksum?(value) digits = value.chars.map { |dgt| dgt =~ /[A-Z]/ ? (dgt.ord - 55) : dgt.to_i } total = 0 digits.each_with_index { |dgt, idx| total += (WEIGHTS[idx] * dgt) } (10 - total % 10) % 10 end def valid_format?(value) value =~ /^([A-Z0-9]{6})(\d{1})$/ end def valid_length?(value) value.present? end def valid?(value) valid_length?(value) && valid_format?(value) && valid_checksum?(value) end end
Version data entries
8 entries across 8 versions & 1 rubygems