struct CPF

Overview

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

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: ...>

Defined in:

cpf_cnpj/cpf/cpf.cr
cpf_cnpj/cpf/errors.cr
cpf_cnpj/cpf/validator.cr
cpf_cnpj/json.cr
cpf_cnpj/yaml.cr

Constructors

Class Method Summary

Instance Method Summary

Instance methods inherited from struct CadastroID

==(other : self) : Bool ==, formatted : String formatted, to_json(json : JSON::Builder) : Nil to_json, to_s(io : IO) : Nil
to_s : String
to_s
, to_yaml(yaml : YAML::Nodes::Builder) : Nil to_yaml, unformatted : String unformatted, value : String value

Constructor methods inherited from struct CadastroID

from_json_object_key?(key : String) : self from_json_object_key?, new(context : YAML::ParseContext, node : YAML::Nodes::Node) : self
new(value : String) : self
new(pull : JSON::PullParser) : self
new

Class methods inherited from struct CadastroID

from_rs(rs : DB::ResultSet) : self | Nil from_rs, parse?(value : String) : self | Nil parse?

Constructor Detail

def self.from_json_object_key?(key : String) : self #

Deserializes the given JSON key into a CPF.

NOTE require "cpf_cnpj/json" is required to opt-in to this feature.


def self.new(context : YAML::ParseContext, node : YAML::Nodes::Node) : self #

Creates CPF from YAML using YAML::ParseContext.

NOTE require "cpf_cnpj/yaml" is required to opt-in to this feature.

require "yaml"
require "cpf_cnpj"
require "cpf_cnpj/yaml"

class Example
  include YAML::Serializable

  property cpf : CPF
end

example = Example.from_yaml("cpf: 466.828.070-40")
example.cpf # => #<CPF: ...>

def self.new(value : String) #

Creates a CPF from a String.

It accepts formatted and stripped strings

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

CPF.new("640.061.830-97") # => #<CPF: ...>
CPF.new("64006183097")    # => #<CPF: ...>
CPF.new("1234")           # => raises `CPF::InvalidValueError`

def self.new(pull : JSON::PullParser) : self #

Creates CPF from JSON using JSON::PullParser.

NOTE require "cpf_cnpj/json" is required to opt-in to this feature.

require "json"
require "cpf_cnpj"
require "cpf_cnpj/json"

class Example
  include JSON::Serializable

  property cpf : CPF
end

example = Example.from_json(%({"cpf": "466.828.070-40"}))
example.cpf # => #<CPF: ...>

Class Method Detail

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

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

CPF.parse?("640.061.830-97") # => #<CPF: ...>
CPF.parse?("64006183097")    # => #<CPF: ...>
CPF.parse?("1234")           # => nil

Instance Method Detail

def formatted : String #

Returns the formatted CPF identifier

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

def unformatted : String #

Returns the unformatted CPF identifier

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

def value : String #

The raw value used to initialize the CPF