The purpose of the lib folder is to have code that doesn't necessary belong in the application but can be shared with other applications. However, we don't have other applications and, if we did, the way to share code between them would be using a gem or even a git submodule. So having both the `app/` and the `lib/` folders is confusing IMHO, and it causes unnecessary problems with autoloading. So we're moving the `lib/` folder to `app/lib/`. Originally, some of these files were in the `app/services/` folder and then they were moved to the `lib/` folder. We're using `app/lib/` instead of `app/services/` so the upgrade is less confusing. There's an exception, though. The `OmniAuth::Strategies::Wordpress` class needs to be available in the Devise initializer. Since this is an initializer and trying to autoload a class here will be problematic when switching to Zeitwerk, we'll keep the `require` clause on top of the Devise initializer in order to load the file and so it will be loaded even if it isn't in the autoload paths anymore.
66 lines
2.1 KiB
Ruby
66 lines
2.1 KiB
Ruby
module DocumentParser
|
|
def get_document_number_variants(document_type, document_number)
|
|
return [] if document_number.blank?
|
|
|
|
# Delete all non-alphanumerics
|
|
document_number = document_number.to_s.gsub(/[^0-9A-Za-z]/i, "")
|
|
variants = []
|
|
|
|
if dni?(document_type)
|
|
document_number, letter = split_letter_from(document_number)
|
|
number_variants = get_number_variants_with_leading_zeroes_from(document_number)
|
|
letter_variants = get_letter_variants(number_variants, letter)
|
|
|
|
variants += number_variants
|
|
variants += letter_variants
|
|
else # if not a DNI, just use the document_number, with no variants
|
|
variants << document_number
|
|
end
|
|
|
|
variants
|
|
end
|
|
|
|
def split_letter_from(document_number)
|
|
letter = document_number.last
|
|
if letter[/[A-Za-z]/] == letter
|
|
document_number = document_number[0..-2]
|
|
else
|
|
letter = nil
|
|
end
|
|
[document_number, letter]
|
|
end
|
|
|
|
# if the number has less digits than it should, pad with zeros to the left and add each variant to the list
|
|
# For example, if the initial document_number is 1234, and digits=8, the result is
|
|
# ['1234', '01234', '001234', '0001234']
|
|
def get_number_variants_with_leading_zeroes_from(document_number, digits = 8)
|
|
document_number = document_number.to_s.last(digits) # Keep only the last x digits
|
|
document_number = document_number.gsub(/^0+/, "") # Removes leading zeros
|
|
|
|
variants = []
|
|
variants << document_number if document_number.present?
|
|
while document_number.size < digits
|
|
document_number = "0#{document_number}"
|
|
variants << document_number
|
|
end
|
|
variants
|
|
end
|
|
|
|
# Generates uppercase and lowercase variants of a series of numbers, if the letter is present
|
|
# If number_variants == ['1234', '01234'] & letter == 'A', the result is
|
|
# ['1234a', '1234A', '01234a', '01234A']
|
|
def get_letter_variants(number_variants, letter)
|
|
variants = []
|
|
if letter.present?
|
|
number_variants.each do |number|
|
|
variants << number + letter.downcase << number + letter.upcase
|
|
end
|
|
end
|
|
variants
|
|
end
|
|
|
|
def dni?(document_type)
|
|
document_type.to_s == "1"
|
|
end
|
|
end
|