lib/br_documents/ie/mg.rb in br_documents-0.1.3 vs lib/br_documents/ie/mg.rb in br_documents-0.2.0

- old
+ new

@@ -1,53 +1,50 @@ -require_relative "base" -require_relative "../commons/mod11" +require_relative 'base' +require_relative '../commons/mod11' module BrDocuments module IE class MG < Base include Commons::Mod11 - private + protected + def format_ie(number) - number.sub(/(\d{3})(\d{3})(\d{3})(\d{4})/, "\\1.\\2.\\3/\\4") + number.sub(/(\d{3})(\d{3})(\d{3})(\d{4})/, '\\1.\\2.\\3/\\4') end def valid_format? regex = /^(\d{3}\.\d{3}\.\d{3}\.\d{2}\-\d{2})$|^(\d{13})$/ regex.match(@number).present? end - def valid_digital_check? - @number.gsub!(/[\.\/-]/, "") + def valid_check_digit? + @number.gsub!(/[\.\/-]/, '') - number1 = @number[0, 3] + "0" + @number[3..10] + number1 = "#{@number[0, 3]}0#{@number[3..10]}" weight1 = [1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2] digital_check1 = generate_first_digital_check(number1, weight1) number2 = "#{@number[0, 11]}#{digital_check1}" weight2 = [3, 2, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2] - digital_check2 = generate_digital_check(number2, weight2) + digital_check2 = generate_check_digit(number2, weight2) - @number[-2, 2].eql? "#{digital_check1}#{digital_check2}" + @number[-2, 2] == "#{digital_check1}#{digital_check2}" end + private + def generate_first_digital_check(values, weights) sum = 0 weights.each_index do |i| number = weights[i] * values[i].to_i - if number < 10 - sum += number - else - sum += number.to_s[0].to_i + number.to_s[1].to_i - end + sum += number < 10 ? number : number.to_s[0].to_i + number.to_s[1].to_i end next_dozen(sum) - sum end def next_dozen(number) - while number % 10 != 0 - number = number + 1 - end + number += 1 while number % 10 != 0 number end end end end