class BraDocumentValidation::CNPJValidator
Constants
- FORMATTED_CNPJ_PATTERN
- NOT_NUMBER_PATTERN
- RAW_CNPJ_PATTERN
Public Instance Methods
validate_each(record, attribute, value)
click to toggle source
# File lib/bra_document_validation/cnpj_validator.rb, line 9 def validate_each(record, attribute, value) return record.errors.add(attribute, error_message(:invalid_format)) unless document_format.match?(value.to_s) full_number = only_numbers_for(value.to_s) record.errors.add(attribute, error_message(:invalid_verification_digit)) if black_listed?(full_number) || !digit_verified?(full_number) end
Private Instance Methods
black_listed?(number)
click to toggle source
# File lib/bra_document_validation/cnpj_validator.rb, line 36 def black_listed?(number) number.chars.uniq.size == 1 end
digit_verified?(full_number)
click to toggle source
# File lib/bra_document_validation/cnpj_validator.rb, line 17 def digit_verified?(full_number) company_number, matrix_subsidiary_number = numbers_for(full_number) full_number == BraDocuments::CNPJGenerator.generate(company_number: company_number, matrix_subsidiary_number: matrix_subsidiary_number) end
document_format()
click to toggle source
# File lib/bra_document_validation/cnpj_validator.rb, line 40 def document_format options[:formatted] ? FORMATTED_CNPJ_PATTERN : RAW_CNPJ_PATTERN end
error_message(default_message)
click to toggle source
# File lib/bra_document_validation/cnpj_validator.rb, line 44 def error_message(default_message) options.fetch(:message, default_message) end
numbers_for(value)
click to toggle source
# File lib/bra_document_validation/cnpj_validator.rb, line 23 def numbers_for(value) number = value.gsub(NOT_NUMBER_PATTERN, '').chars [ number.shift(BraDocuments::CNPJGenerator::COMPANY_NUMBER_SIZE).join, number.shift(BraDocuments::CNPJGenerator::MATRIX_SUBSIDIARY_SIZE).join ] end
only_numbers_for(value)
click to toggle source
# File lib/bra_document_validation/cnpj_validator.rb, line 32 def only_numbers_for(value) value.gsub(NOT_NUMBER_PATTERN, '') end