Class: ValidatesIdentity::BrCnpj::Validator
- Inherits:
-
Object
- Object
- ValidatesIdentity::BrCnpj::Validator
- Defined in:
- lib/validates_identity/br_cnpj/validator.rb
Constant Summary collapse
- VALIDATION_REGULAR_EXPRESSION =
%r{^(\d{2}\.?\d{3}\.?\d{3}/?\d{4})-?(\d{2})$}.freeze
- FORMAT_REGULAR_EXPRESSION =
/(\d{2})(\d{3})(\d{3})(\d{4})(\d{2})/.freeze
Instance Attribute Summary collapse
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
- #formatted ⇒ Object
-
#initialize(value) ⇒ Validator
constructor
A new instance of Validator.
- #valid? ⇒ Boolean
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
#value ⇒ Object (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
#formatted ⇒ Object
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
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.scan(/\d/).uniq.length == 1 verifier_digits == "#{first_digit_verifier}#{second_digit_verifier}" end |