Class: CNPJ::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/cnpj/formatter.rb

Constant Summary collapse

STRICT_REGEX =
%r{[/.-]}.freeze
LOOSE_REGEX =
/[^A-Z\d]/.freeze
REPLACE_REGEX =
/
  \A([A-Z\d]{2})([A-Z\d]{3})([A-Z\d]{3})([A-Z\d]{4})([A-Z\d]{2})\Z
/x.freeze

Class Method Summary collapse

Class Method Details

.format(number) ⇒ Object



11
12
13
# File 'lib/cnpj/formatter.rb', line 11

def self.format(number)
  strip(number).gsub(REPLACE_REGEX, "\\1.\\2.\\3/\\4-\\5")
end

.strip(number, strict = false) ⇒ Object



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

def self.strip(number, strict = false)
  number.to_s.gsub(strict ? STRICT_REGEX : LOOSE_REGEX, "")
end