Permalink
Browse files

Update for US format. Updated Readme. Hungary number bug fix

  • Loading branch information...
carr committed Jan 6, 2010
1 parent 9826c61 commit f923cce56b09f7d5ab066f3dd80c43dc88b0f650
Showing with 19 additions and 14 deletions.
  1. +3 −2 Readme.rdoc
  2. +1 −0 data/countries.yml
  3. +15 −12 lib/phone.rb
View
@@ -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 <tt>Phone.named_formats</tt> 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,
View
@@ -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"
View
@@ -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?

0 comments on commit f923cce

Please sign in to comment.