abstract struct CadastroID
- CadastroID
- Struct
- Value
- Object
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.crcpf_cnpj/cadastro_id/errors.cr
cpf_cnpj/db.cr
cpf_cnpj/json.cr
cpf_cnpj/yaml.cr
Constructors
- .from_json_object_key?(key : String) : self
- .new(context : YAML::ParseContext, node : YAML::Nodes::Node) : self
- 
        .new(value : String) : self
        
          Attempts to instantiate a CPF or CNPJ. 
- .new(pull : JSON::PullParser) : self
Class Method Summary
- .from_rs(rs : DB::ResultSet) : self | Nil
- 
        .parse?(value : String) : self | Nil
        
          Attempts to instantiate a CPF or CNPJ. 
Instance Method Summary
- 
        #==(other : self) : Bool
        
          Returns true if the identifiers are equal, ignoring formatting. 
- 
        #formatted : String
        
          Returns the formatted value 
- #to_json(json : JSON::Builder) : Nil
- 
        #to_s(io : IO) : Nil
        
          Appends #valueto io.
- 
        #to_s : String
        
          Returns the identifier provived on initialization. 
- #to_yaml(yaml : YAML::Nodes::Builder) : Nil
- 
        #unformatted : String
        
          Returns the unformatted value 
- 
        #value : String
        
          Returns the value provived on initialization 
Constructor Detail
Deserializes the given JSON key into a CPF or CNPJ.
NOTE  require "cpf_cnpj/json" is required to opt-in to this feature.
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: ...>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`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
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
endAttempts 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) # => nilInstance Method Detail
Returns true if the identifiers are equal, ignoring formatting.
CPF.new("592.736.970-70") == CPF.new("59273697070") # => trueReturns 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\""