Class: HappyPhoneNumber::Mask

Inherits:
Object
  • Object
show all
Defined in:
lib/happy_phone_number/mask.rb

Overview

Internal: Phone number formatting using a mask.

Examples

Mask.new("123456", "### ###").format
# => "123 456"

Mask.new("123456", "## ##-##").format
# => "12 34-56"

Mask.new("123456", "##.####").format
# => "12.3456"

Instance Method Summary collapse

Constructor Details

#initialize(phone, mask) ⇒ Mask

Internal: Initialize a new mask formatter.

phone - A String phone number. mask - A String mask.



23
24
25
26
# File 'lib/happy_phone_number/mask.rb', line 23

def initialize phone, mask
  @phone = phone
  @mask = mask
end

Instance Method Details

#formatObject

Internal: Format the phone number. If the mask is too long for the phone number, it will be truncated (the mask). If the mask is too short, the returned phone number will be left untouched.

Examples

Mask.new("1234", "### ###").format
# => "123 4"

Mask.new("1234", "#").format
# => "1234"

Returns the String formatted phone number.



41
42
43
44
45
46
47
48
49
50
# File 'lib/happy_phone_number/mask.rb', line 41

def format
  return @phone if mask_too_short?
  mask_array.map do |item|
    if item == "#"
      next_digit
    else
      item
    end
  end.join
end