Class: ValidatesIdentity::BrCnpj::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/validates_identity/br_cnpj/validator.rb

Constant Summary collapse

VALIDATION_REGULAR_EXPRESSION =
%r{^([A-Z\d]{2}\.?[A-Z\d]{3}\.?[A-Z\d]{3}/?[A-Z\d]{4})-?(\d{2})$}.freeze
FORMAT_REGULAR_EXPRESSION =
/([A-Z\d]{2})([A-Z\d]{3})([A-Z\d]{3})([A-Z\d]{4})(\d{2})/.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Validator

Returns a new instance of Validator.



11
12
13
# File 'lib/validates_identity/br_cnpj/validator.rb', line 11

def initialize(value)
  @value = value
end

Instance Attribute Details

#valueObject (readonly)

Returns the value of attribute value.



9
10
11
# File 'lib/validates_identity/br_cnpj/validator.rb', line 9

def value
  @value
end

Instance Method Details

#formattedObject



24
25
26
27
28
29
# File 'lib/validates_identity/br_cnpj/validator.rb', line 24

def formatted
  return if number.nil?

  result = FORMAT_REGULAR_EXPRESSION.match(striped_value)
  "#{result[1]}.#{result[2]}.#{result[3]}/#{result[4]}-#{result[5]}"
end

#valid?Boolean

Returns:

  • (Boolean)


15
16
17
18
19
20
21
22
# File 'lib/validates_identity/br_cnpj/validator.rb', line 15

def valid?
  return true if value.blank?
  return false unless number
  return false if striped_value.length != 14
  return false if striped_value[0, 12].chars.uniq.length == 1

  verifier_digits == "#{first_digit_verifier}#{second_digit_verifier}"
end