class CPF

Overview

Represents a CPF (Cadastro de Pessoas FĂ­sicas) number.

A CPF object is designed to never hold an invalid value, so you can assume that a CPF object will always hold a valid value.

cpf = CPF.new("640.061.830-97")
cpf.value       # => "640.061.830-97"
cpf.formatted   # => "640.061.830-97"
cpf.unformatted # => "64006183097"

CPF.new("11111111111") # => raises `ArgumentError`

CPF.parse("11111111111")    # => nil
CPF.parse("640.061.830-97") # => #<CPF:0x104fe0ae0 @value="640.061.830-97">

Defined in:

cpf.cr
cpf/validator.cr

Constant Summary

VERSION = "1.0.0"

Constructors

Class Method Summary

Instance Method Summary

Constructor Detail

def self.new(value : String) : CPF #

Creates a CPF from a String.

It accepts formatted and stripped strings

If the String isn't a valid CPF, an ArgumentError exception will be raised. See .parse if you want a safe way to initialize a CPF.


[View source]

Class Method Detail

def self.parse(value : String) : CPF | Nil #

Returns a CPF if the given String is a valid CPF number, otherwise returns nil.


[View source]

Instance Method Detail

def formatted : String #

Returns the formatted CPF number

cpf = CPF.new("64006183097")
cpf.formatted # => "640.061.830-97"

[View source]
def unformatted : String #

Returns the unformatted CPF number

cpf = CPF.new("640.061.830-97")
cpf.unformatted # => "64006183097"

[View source]
def value : String #

Returns the value provived on initialization

cpf = CPF.new("640.061.830-97")
cpf.value # => "640.061.830-97"

[View source]