Sha256: faec762430b5ea87c521220cfc62c9ed2b52b67c5731beee30c61b61a5c0aa8f
Contents?: true
Size: 685 Bytes
Versions: 2
Compression:
Stored size: 685 Bytes
Contents
# frozen_string_literal: true class CusipValidator < BaseValidator REGEXP = /^[0-9A-Z]{9}$/.freeze private def valid_attr? valid_regexp? && valid_checksum? end # rubocop:disable Metrics/AbcSize def valid_checksum? digits = value.chars.map { |chr| /[A-Z]/.match?(chr) ? (chr.ord - 55) : chr.to_i } even_values = digits.values_at(*digits.each_index.select(&:even?)) odd_values = digits.values_at(*digits.each_index.select(&:odd?)) values = odd_values.map { |int| int * 2 }.zip(even_values).flatten values = values.inject(0) { |sum, int| sum + (int / 10) + int % 10 } ((10 - values) % 10) % 10 end # rubocop:enable Metrics/AbcSize end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
lite-validators-1.2.0 | lib/lite/validators/cusip_validator.rb |
lite-validators-1.1.2 | lib/lite/validators/cusip_validator.rb |