Sha256: 1de8731c4946f2e285093a30123efb0ec1b94be866563fb2db18cff9d5a3b520
Contents?: true
Size: 687 Bytes
Versions: 2
Compression:
Stored size: 687 Bytes
Contents
class SedolValidator < ActiveModel::EachValidator def validate_each(record, attribute, value) unless valid?(value) record.errors[attribute] << (options[:message] || I18n.t('active_validation.errors.messages.sedol')) end end private def valid_format?(value) value =~ /^([A-Z0-9]{6})(\d{1})$/ end def valid_checksum?(value) digits = value.split('').map { |i| i.match(/[A-Z]/) ? (i.ord - 55) : i.to_i } weights = [1, 3, 1, 7, 3, 9, 1] sum = 0 digits.each_with_index do |i, idx| sum += weights[idx] * i end (10 - sum % 10) % 10 end def valid?(value) valid_format?(value) && valid_checksum?(value) end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
active_validation-1.1.0 | lib/active_validation/validators/sedol_validator.rb |
active_validation-1.0.0 | lib/active_validation/validators/sedol_validator.rb |