Class: DataMapper::Address::PhoneNumber

Inherits:
String
  • Object
show all
Defined in:
lib/dm-address/phone_number.rb

Constant Summary collapse

DEFAULT_FORMAT =
'(%A) %P-%S'

Instance Method Summary collapse

Constructor Details

#initialize(s) ⇒ PhoneNumber

Remove all non-digits from given phone number



7
8
9
# File 'lib/dm-address/phone_number.rb', line 7

def initialize(s)
  super((s || '').to_str.gsub(/\D+/, ''))
end

Instance Method Details

#to_s(format = nil) ⇒ Object

%A is area code, %P is prefix, %S is last 4 digits (suffix) Default is ā€œ(%A) %P-%Sā€ -> (###) ###-####



13
14
15
16
17
18
19
20
21
# File 'lib/dm-address/phone_number.rb', line 13

def to_s(format = nil)
  unless format
    format = DataMapper::Address.config[:phone_format] || DEFAULT_FORMAT
  end
  return '' if to_str.empty?
  format.gsub(/\%A/, to_str[0..2]).
         gsub(/\%P/, to_str[3..5]).
         gsub(/\%S/, to_str[6..9])
end