From f923cce56b09f7d5ab066f3dd80c43dc88b0f650 Mon Sep 17 00:00:00 2001 From: carr Date: Wed, 6 Jan 2010 12:55:55 +0100 Subject: [PATCH] Update for US format. Updated Readme. Hungary number bug fix --- Readme.rdoc | 5 +++-- data/countries.yml | 1 + lib/phone.rb | 27 +++++++++++++++------------ 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/Readme.rdoc b/Readme.rdoc index 51136a8..f489150 100644 --- a/Readme.rdoc +++ b/Readme.rdoc @@ -70,11 +70,12 @@ When given a string, it interpolates the string with the following fields: pn.format("+ %c (%a) %n") # => "+ 385 (91) 5125486" When given a symbol it is used as a lookup for the format in the Phone.named_formats hash. - pn.format(:europe) + pn.format(:europe) # => "+385 (0) 91 512 5486" + pn.format(:us) # => "(234) 123 4567" You can add your own custom named formats like so: Phone.named_formats[:short] = '%A/%n1-%n2' - pn.format(:short) + pn.format(:short) # => 091/512-5486 = TODO Parse testing for different countries. Currently tested on: US, Croatia, Slovenia, Hungary, Serbia, Bosnia and Herzegovina, diff --git a/data/countries.yml b/data/countries.yml index 8d11236..449eb54 100644 --- a/data/countries.yml +++ b/data/countries.yml @@ -741,6 +741,7 @@ :char_3_code: HU :name: Hungary :international_dialing_prefix: "0" + :area_code: "1|[2-9][0-9]" "263": :country_code: "263" :national_dialing_prefix: "0" diff --git a/lib/phone.rb b/lib/phone.rb index 3d15550..9dc13ee 100644 --- a/lib/phone.rb +++ b/lib/phone.rb @@ -27,7 +27,9 @@ class Phone @@n1_length = 3 @@named_formats = { - :europe => '+%c (0) %a %f %l' + :default => "+%c%a%n", + :europe => '+%c (0) %a %f %l', + :us => "(%a) %f-%l" } def initialize(*hash_or_args) @@ -161,18 +163,19 @@ def number2 number[-n2_length, n2_length] end - # formats phone number. + # Formats the phone number. # - # if the method argument is a Symbol, it is used as a lookup key in Phone.named_formats - # pn.format(:europe) + # if the method argument is a String, it is used as a format string, with the following fields being interpolated: + # + # * %c - country_code (385) + # * %a - area_code (91) + # * %A - area_code with leading zero (091) + # * %n - number (5125486) + # * %n1 - first @@n1_length characters of number (configured through Phone.n1_length), default is 3 (512) + # * %n2 - last characters of number (5486) # - # if the method argument is a String, it is used as a format string, with the following fields being interpolated: - # %c - country_code (385) - # %a - area_code (91) - # %A - area_code with leading zero (091) - # %n - number (5125486) - # %n1 - first @@n1_length characters of number (configured through Phone.n1_length), default is 3 (512) - # %n2 - last characters of number (5486) + # if the method argument is a Symbol, it is used as a lookup key for a format String in Phone.named_formats + # pn.format(:europe) def format(fmt) if fmt.is_a?(Symbol) raise "The format #{fmt} doesn't exist'" unless named_formats.has_key?(fmt) @@ -184,7 +187,7 @@ def format(fmt) # the default format is "+%c%a%n" def to_s - format("+%c%a%n") + format(:default) end # does this number belong to the default country code?