Skip to content

Commit

Permalink
Update for US format. Updated Readme. Hungary number bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
carr committed Jan 6, 2010
1 parent 9826c61 commit f923cce
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
5 changes: 3 additions & 2 deletions Readme.rdoc
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions data/countries.yml
Expand Up @@ -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"
Expand Down
27 changes: 15 additions & 12 deletions lib/phone.rb
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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?
Expand Down

0 comments on commit f923cce

Please sign in to comment.