Sha256: 639b04d15e9de5f51c5a9ce2aae2be5ce5bcec6d942ba16a0ec500976b6cc175

Contents?: true

Size: 519 Bytes

Versions: 15

Compression:

Stored size: 519 Bytes

Contents

class Unidom::Certificate::China::IdentificationNumberValidator < ActiveModel::EachValidator

  WEIGHTS      = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 ].freeze
  CHECK_DIGITS = %w{1 0 X 9 8 7 6 5 4 3 2}.freeze

  def validate_each(record, attribute, value)

    value = value.to_s
    sum   = 0
    value[0..16].chars.each_with_index do |char, index| sum += char.to_i*WEIGHTS[index] end

    record.errors[attribute] << (options[:message]||'is invalid') unless CHECK_DIGITS[sum%11]==value[17]

  end

end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
unidom-certificate-china-2.0 app/validators/unidom/certificate/china/identification_number_validator.rb
unidom-certificate-china-1.5.6 app/validators/unidom/certificate/china/identification_number_validator.rb
unidom-certificate-china-1.5.5 app/validators/unidom/certificate/china/identification_number_validator.rb
unidom-certificate-china-1.5.4 app/validators/unidom/certificate/china/identification_number_validator.rb
unidom-certificate-china-1.5.3 app/validators/unidom/certificate/china/identification_number_validator.rb
unidom-certificate-china-1.5.2 app/validators/unidom/certificate/china/identification_number_validator.rb
unidom-certificate-china-1.5.1 app/validators/unidom/certificate/china/identification_number_validator.rb
unidom-certificate-china-1.5 app/validators/unidom/certificate/china/identification_number_validator.rb
unidom-certificate-china-1.4 app/validators/unidom/certificate/china/identification_number_validator.rb
unidom-certificate-china-1.3 app/validators/unidom/certificate/china/identification_number_validator.rb
unidom-certificate-china-1.2.4 app/validators/unidom/certificate/china/identification_number_validator.rb
unidom-certificate-china-1.2.3 app/validators/unidom/certificate/china/identification_number_validator.rb
unidom-certificate-china-1.2.2 app/validators/unidom/certificate/china/identification_number_validator.rb
unidom-certificate-china-1.2.1 app/validators/unidom/certificate/china/identification_number_validator.rb
unidom-certificate-china-1.2 app/validators/unidom/certificate/china/identification_number_validator.rb