Sha256: ce4600a813c2979fd7b226433755a4472f0442abb7458c02a9a3c566e547d5a6

Contents?: true

Size: 461 Bytes

Versions: 3

Compression:

Stored size: 461 Bytes

Contents

# frozen_string_literal: true

class SedolValidator < BaseValidator

  REGEXP = /^([A-Z0-9]{6})(\d{1})$/
  WEIGHTS = [
    1, 3, 1, 7, 3, 9, 1
  ].freeze

  private

  def valid_attr?
    valid_regexp? && valid_checksum?
  end

  def valid_checksum?
    total = 0
    digits = value.chars.map { |dgt| /[A-Z]/.match?(dgt) ? (dgt.ord - 55) : dgt.to_i }
    digits.each_with_index { |dgt, idx| total += (WEIGHTS[idx] * dgt) }
    (10 - total % 10) % 10
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
lite-validators-1.3.2 lib/lite/validators/sedol_validator.rb
lite-validators-1.3.1 lib/lite/validators/sedol_validator.rb
lite-validators-1.3.0 lib/lite/validators/sedol_validator.rb