lib/data_maker/cn/nin.rb in data_maker-2.4.1 vs lib/data_maker/cn/nin.rb in data_maker-2.4.2

- old
+ new

@@ -7,54 +7,30 @@ nin = GenerateNationalIDNumber.new nin.generate end class GenerateNationalIDNumber - attr_accessor :birthdate - - def initialize - self.birthdate = generate_birthdate - end - def generate nin = national_id_number - while DataMaker::Validators::ChineseNINValidator.valid?(nin) - nin = national_id_number - end + # while DataMaker::Validators::ChineseNINValidator.valid?(nin) + # nin = national_id_number + # break if DataMaker::Validators::ChineseNINValidator.valid?(nin) + # end nin end private - def generate_birthdate - rand(1920..1998).to_s + rand(1..12).to_s.rjust(2, '0') + rand(1..30).to_s.rjust(2, '0') - end - - def base_number - rand(10 ** 6).to_s.rjust(6, '0') + birthdate + rand(10 ** 3).to_s.rjust(3, '0') - end - def national_id_number - self.birthdate = generate_birthdate - base_number + DataMaker::CN::NIN::GenerateChecksum.new(base_number).generate - end - end - - class GenerateChecksum - attr_accessor :base_number - - def initialize(base_number) - self.base_number = base_number - end - - def generate + birthdate = rand(1920..1998).to_s + rand(1..12).to_s.rjust(2, '0') + rand(1..30).to_s.rjust(2, '0') + base_number = rand(10 ** 6).to_s.rjust(6, '0') + birthdate + rand(10 ** 3).to_s.rjust(3, '0') weights = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2] - summod = [1, 0, "X", 9, 8, 7, 6, 5, 4, 3, 2] + summod = [1, 0, "X", 9, 8, 7, 6, 5, 4, 3, 2] checksum = 0 - weights.each_with_index do |weight, index| - checksum += (weight.to_i * base_number[index].to_i) + weights.each_with_index do |weight, i| + checksum += (weight * base_number[i].to_i) end - summod[(checksum % 11)].to_s + base_number + summod[(checksum % 11)].to_s end end end end end