Sha256: 90c7e9cc32f1fa6fe313cbd4210ccb20c601fa05c1d9d335442754e76de31f99

Contents?: true

Size: 687 Bytes

Versions: 10

Compression:

Stored size: 687 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

10 entries across 10 versions & 1 rubygems

Version Path
lite-validators-1.1.1 lib/lite/validators/cusip_validator.rb
lite-validators-1.1.0 lib/lite/validators/cusip_validator.rb
lite-validators-1.0.7 lib/lite/validators/cusip_validator.rb
lite-validators-1.0.6 lib/lite/validators/cusip_validator.rb
lite-validators-1.0.5 lib/lite/validators/cusip_validator.rb
lite-validators-1.0.4 lib/lite/validators/cusip_validator.rb
lite-validators-1.0.3 lib/lite/validators/cusip_validator.rb
lite-validators-1.0.2 lib/lite/validators/cusip_validator.rb
lite-validators-1.0.1 lib/lite/validators/cusip_validator.rb
lite-validators-1.0.0 lib/lite/validators/cusip_validator.rb