Class: CPF::Formatter

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

Constant Summary collapse

STRICT_REGEX =
/[.-]/.freeze
LOOSE_REGEX =
/[^\d]/.freeze

Class Method Summary collapse

Class Method Details

.format(number) ⇒ Object



8
9
10
# File 'lib/cpf/formatter.rb', line 8

def self.format(number)
  strip(number).gsub(/\A(\d{3})(\d{3})(\d{3})(\d{2})\Z/, "\\1.\\2.\\3-\\4")
end

.strip(number, strict = false) ⇒ Object



12
13
14
# File 'lib/cpf/formatter.rb', line 12

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