Sha256: 795cf054974d4b2a18f780a615dee8e75aede4ab674ccbc5a0c5c116456f9ba4
Contents?: true
Size: 750 Bytes
Versions: 1
Compression:
Stored size: 750 Bytes
Contents
class SedolValidator < ActiveModel::EachValidator def validate_each(record, attribute, value) unless valid?(value.to_s) record.errors[attribute] << options.fetch(:message, I18n.t("active_validation.errors.messages.sedol")) end end private def valid_checksum?(value) digits = value.chars.map { |d| d.match(/[A-Z]/) ? (d.ord - 55) : d.to_i } weights = [1, 3, 1, 7, 3, 9, 1] total = 0 digits.each_with_index { |d, i| total += (weights[i] * d) } (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
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
active_validation-3.0.0 | lib/active_validation/validators/sedol_validator.rb |