Class: Unidom::Certificate::China::IdentificationNumberValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
app/validators/unidom/certificate/china/identification_number_validator.rb

Constant Summary collapse

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

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'app/validators/unidom/certificate/china/identification_number_validator.rb', line 6

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