abstract struct CadastroID

Overview

CadastroID is an abstract struct, so you cannot instantiate it directly. It is useful when a variable may hold a CPF or a CNPJ value. It provides the .new and .parse? methods, just like CPF and CNPJ:

id_a = CadastroID.new("640.061.830-97")
id_a.value   # => "640.061.830-97"
id_a.class   # => CPF
typeof(id_a) # => CadastroID

id_b = CadastroID.new("VCZ83T1R000106")
id_b.value   # => "VCZ83T1R000106"
id_b.class   # => CNPJ
typeof(id_b) # => CadastroID

# With invalid value
CadastroID.new("1234") # => raises `CadastroID::InvalidValueError`

# Safely instantiating a CPF or CNPJ
CadastroID.parse?("1234")               # => nil
CadastroID.parse?("24.485.147/0001-87") # => #<CNPJ: ...>

Direct Known Subclasses

Defined in:

cpf_cnpj/cadastro_id/cadastro_id.cr
cpf_cnpj/cadastro_id/errors.cr
cpf_cnpj/db.cr
cpf_cnpj/json.cr
cpf_cnpj/yaml.cr

Constructors

Class Method Summary

Instance Method Summary

Constructor Detail

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

Deserializes the given JSON key into a CPF or CNPJ.

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 or CNPJ 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 id : CadastroID
end

example = Example.from_yaml("id: 7B.N1F.Y9N/0001-98")
example.id # => #<CNPJ: ...>

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

Attempts to instantiate a CPF or CNPJ. Raises CadastroID::InvalidValueError if the value is invalid.

CadastroID.new("VC.Z83.T1R/0001-06") # => #<CNPJ: ...>
CadastroID.new("592.736.970-70") # => #<CPF: ...>
CadastroID.new("1234) # => raises `CadastroID::InvalidValueError`

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

Creates CPF or CNPJ 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 id : CadastroID
end

example = Example.from_json(%({"id": "7B.N1F.Y9N/0001-98"}))
example.id # => #<CNPJ: ...>

Class Method Detail

def self.from_rs(rs : DB::ResultSet) : self | Nil #

Create an instance of CPF or CNPJ from a DB::ResultSet. You need to use the @[DB::Field] annotation and provide CPF, CNPJ or CadastroID as a converter.

Note: require "cpf_cnpj/db" is required to opt-in to this feature

If you have NULL, empty or invalid values in database, you should use nilable types, eg. CPF?, CNPJ? or CadastroID?.

require "cpf_cnpj/db"

class Example
  include DB::Serializable

  @[DB::Field(converter: CadastroID)]
  property identifier : CadastroID

  @[DB::Field(converter: CNPJ)]
  property cnpj : CNPJ

  @[DB::Field(converter: CPF)]
  property cpf : CPF
end

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

Attempts to instantiate a CPF or CNPJ. Returns nil if the value is invalid.

CadastroID.parse?("VC.Z83.T1R/0001-06") # => <CNPJ ...>
CadastroID.parse?("592.736.970-70") # => <CPF ...>
CadastroID.parse?("1234) # => nil

Instance Method Detail

def ==(other : self) : Bool #

Returns true if the identifiers are equal, ignoring formatting.

CPF.new("592.736.970-70") == CPF.new("59273697070") # => true

abstract def formatted : String #

Returns the formatted value


def to_json(json : JSON::Builder) : Nil #

Returns CPF/CNPJ identifier as JSON value.

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

identifier = CadastroID.new("7B.N1F.Y9N/0001-98")
identifier.to_json # => "\"7B.N1F.Y9N/0001-98\""

def to_s(io : IO) : Nil #

Appends #value to io.


def to_s : String #

Returns the identifier provived on initialization. Equivalent to #value.


def to_yaml(yaml : YAML::Nodes::Builder) : Nil #

Returns CPF or CNPJ identifier as YAML value.

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

identifier = CadastroID.new("7B.N1F.Y9N/0001-98")
identifier.to_yaml # => "--- 7B.N1F.Y9N/0001-98\n"

abstract def unformatted : String #

Returns the unformatted value


abstract def value : String #

Returns the value provived on initialization