Module: CpfCnpjPlus

Defined in:
lib/cpf_cnpj_plus.rb,
lib/cpf_cnpj_plus/version.rb,
lib/cpf_cnpj_plus/formatter.rb,
lib/cpf_cnpj_plus/generator.rb,
lib/cpf_cnpj_plus/validator.rb,
lib/cpf_cnpj_plus/formatter/cpf.rb,
lib/cpf_cnpj_plus/generator/cpf.rb,
lib/cpf_cnpj_plus/validator/cpf.rb,
lib/cpf_cnpj_plus/formatter/cnpj.rb,
lib/cpf_cnpj_plus/generator/cnpj.rb,
lib/cpf_cnpj_plus/validator/cnpj.rb,
sig/cpf_cnpj_plus.rbs

Overview

Módulo principal da gem cpf_cnpj_plus. Fornece métodos para validação, geração e formatação de CPFs e CNPJs, incluindo suporte ao novo padrão de CNPJ alfanumérico.

Defined Under Namespace

Modules: Formatter, Generator, Validator Classes: Error

Constant Summary collapse

VERSION =

Returns:

  • (String)
"0.4.0"

Class Method Summary collapse

Class Method Details

.alphanumeric_cnpj?(cnpj) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/cpf_cnpj_plus.rb', line 41

def self.alphanumeric_cnpj?(cnpj)
  CpfCnpjPlus::Validator::Cnpj.alphanumeric?(cnpj)
end

.format(doc, strict: false) ⇒ Object



55
56
57
58
59
60
# File 'lib/cpf_cnpj_plus.rb', line 55

def self.format(doc, strict: false)
  case document_type(doc)
  when :cpf then format_cpf(doc, strict: strict)
  when :cnpj then format_cnpj(doc, strict: strict)
  end
end

.format_cnpj(cnpj, strict: false) ⇒ Object



33
34
35
# File 'lib/cpf_cnpj_plus.rb', line 33

def self.format_cnpj(cnpj, strict: false)
  CpfCnpjPlus::Formatter::Cnpj.format(cnpj, strict: strict)
end

.format_cpf(cpf, strict: false) ⇒ Object



19
20
21
# File 'lib/cpf_cnpj_plus.rb', line 19

def self.format_cpf(cpf, strict: false)
  CpfCnpjPlus::Formatter::Cpf.format(cpf, strict: strict)
end

.generate(type, alphanumeric: false) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/cpf_cnpj_plus.rb', line 62

def self.generate(type, alphanumeric: false)
  case type.to_s.downcase
  when "cpf" then generate_cpf
  when "cnpj" then generate_cnpj(alphanumeric: alphanumeric)
  else
    raise ArgumentError, "type deve ser :cpf ou :cnpj (recebido: #{type.inspect})"
  end
end

.generate_cnpj(alphanumeric: false) ⇒ Object



37
38
39
# File 'lib/cpf_cnpj_plus.rb', line 37

def self.generate_cnpj(alphanumeric: false)
  CpfCnpjPlus::Generator::Cnpj.generate(alphanumeric: alphanumeric)
end

.generate_cpfObject



23
24
25
# File 'lib/cpf_cnpj_plus.rb', line 23

def self.generate_cpf
  CpfCnpjPlus::Generator::Cpf.generate
end

.valid?(doc) ⇒ Boolean

API unificada (detecta CPF ou CNPJ pelo tamanho do documento)

Returns:

  • (Boolean)


47
48
49
50
51
52
53
# File 'lib/cpf_cnpj_plus.rb', line 47

def self.valid?(doc)
  case document_type(doc)
  when :cpf then valid_cpf?(doc)
  when :cnpj then valid_cnpj?(doc)
  else false
  end
end

.valid_cnpj?(cnpj) ⇒ Boolean

CNPJ

Returns:

  • (Boolean)


29
30
31
# File 'lib/cpf_cnpj_plus.rb', line 29

def self.valid_cnpj?(cnpj)
  CpfCnpjPlus::Validator::Cnpj.valid?(cnpj)
end

.valid_cpf?(cpf) ⇒ Boolean

CPF

Returns:

  • (Boolean)


15
16
17
# File 'lib/cpf_cnpj_plus.rb', line 15

def self.valid_cpf?(cpf)
  CpfCnpjPlus::Validator::Cpf.valid?(cpf)
end