Skip to content

Commit

Permalink
Version 1.0 - fixed namespacing
Browse files Browse the repository at this point in the history
  • Loading branch information
carr committed Apr 5, 2011
1 parent 6e67aa5 commit 4592f04
Show file tree
Hide file tree
Showing 11 changed files with 370 additions and 355 deletions.
51 changes: 30 additions & 21 deletions Readme.rdoc
Expand Up @@ -9,34 +9,40 @@ You can install the phone library as a gem
Or as a Rails plugin Or as a Rails plugin
script/plugin install git://github.com/carr/phone.git script/plugin install git://github.com/carr/phone.git


== Updates in v 1.0

The biggest updating is the namespacing problem fixed that a lot of people were having. You now use phone by refering to

Phoner::Phone

== Initializing == Initializing
You can initialize a new phone object with the number, area code, country code and extension number You can initialize a new phone object with the number, area code, country code and extension number


Phone.new('5125486', '91', '385') Phoner::Phone.new('5125486', '91', '385')
or or
Phone.new(:number => '5125486', :area_code => '91', :country_code => '385', :extension => '143') Phoner::Phone.new(:number => '5125486', :area_code => '91', :country_code => '385', :extension => '143')


== Parsing == Parsing
You can create a new phone object by parsing from a string. Phone does it's best to detect the country and area codes: You can create a new phone object by parsing from a string. Phoner::Phone does it's best to detect the country and area codes:
Phone.parse '+385915125486' Phoner::Phone.parse '+385915125486'
Phone.parse '00385915125486' Phoner::Phone.parse '00385915125486'


If the country or area code isn't given in the string, you must set it, otherwise it doesn't work: If the country or area code isn't given in the string, you must set it, otherwise it doesn't work:
Phone.parse '091/512-5486', :country_code => '385' Phoner::Phone.parse '091/512-5486', :country_code => '385'
Phone.parse '(091) 512 5486', :country_code => '385' Phoner::Phone.parse '(091) 512 5486', :country_code => '385'


If you feel that it's tedious, set the default country code once (in your config/environment.rb): If you feel that it's tedious, set the default country code once (in your config/environment.rb):
Phone.default_country_code = '385' Phoner::Phone.default_country_code = '385'
Phone.parse '091/512-5486' Phoner::Phone.parse '091/512-5486'
Phone.parse '(091) 512 5486' Phoner::Phone.parse '(091) 512 5486'


Same goes for the area code: Same goes for the area code:
Phone.parse '451-588', :country_code => '385', :area_code => '47' Phoner::Phone.parse '451-588', :country_code => '385', :area_code => '47'
or or
Phone.default_country_code = '385' Phoner::Phone.default_country_code = '385'
Phone.default_area_code = '47' Phoner::Phone.default_area_code = '47'


Phone.parse '451-588' Phoner::Phone.parse '451-588'


=== Automatic country and area code detection === Automatic country and area code detection
Like it's stated above, Phone does it's best to automatically detect the country and area code while parsing. Do do this, Like it's stated above, Phone does it's best to automatically detect the country and area code while parsing. Do do this,
Expand All @@ -45,12 +51,12 @@ phone uses data stored in <tt>data/countries.yml</tt>.
Each country code can have a regular expression named <tt>area_code</tt> that describes how the area code for that Each country code can have a regular expression named <tt>area_code</tt> that describes how the area code for that
particular country looks like. particular country looks like.


If an <tt>area_code</tt> regular expression isn't specified, the default, <tt>Phone::DEFAULT_AREA_CODE</tt> (correct for If an <tt>area_code</tt> regular expression isn't specified, the default, <tt>Phoner::Phone::DEFAULT_AREA_CODE</tt> (correct for
the US) is used. the US) is used.


== Validating == Validating
Validating is very relaxed, basically it strips out everything that's not a number or '+' character: Validating is very relaxed, basically it strips out everything that's not a number or '+' character:
Phone.valid? 'blabla 091/512-5486 blabla' Phoner::Phone.valid? 'blabla 091/512-5486 blabla'


== Formatting == Formatting
Formating is done via the <tt>format</tt> method. The method accepts a <tt>Symbol</tt> or a <tt>String</tt>. Formating is done via the <tt>format</tt> method. The method accepts a <tt>Symbol</tt> or a <tt>String</tt>.
Expand All @@ -61,22 +67,22 @@ When given a string, it interpolates the string with the following fields:
* %a - area_code (91) * %a - area_code (91)
* %A - area_code with leading zero (091) * %A - area_code with leading zero (091)
* %n - number (5125486) * %n - number (5125486)
* %f - first @@n1_length characters of number (configured through Phone.n1_length), default is 3 (512) * %f - first @@n1_length characters of number (configured through Phoner::Phone.n1_length), default is 3 (512)
* %l - last characters of number (5486) * %l - last characters of number (5486)
* %x - the extension number * %x - the extension number


pn = Phone.parse('+385915125486') pn = Phoner::Phone.parse('+385915125486')
pn.to_s # => "+385915125486" pn.to_s # => "+385915125486"
pn.format("%A/%f-%l") # => "091/512-5486" pn.format("%A/%f-%l") # => "091/512-5486"
pn.format("+ %c (%a) %n") # => "+ 385 (91) 5125486" 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. When given a symbol it is used as a lookup for the format in the <tt>Phoner::Phone.named_formats</tt> hash.
pn.format(:europe) # => "+385 (0) 91 512 5486" pn.format(:europe) # => "+385 (0) 91 512 5486"
pn.format(:us) # => "(234) 123 4567" pn.format(:us) # => "(234) 123 4567"
pn.format(:default_with_extension) # => "+3851234567x143" pn.format(:default_with_extension) # => "+3851234567x143"


You can add your own custom named formats like so: You can add your own custom named formats like so:
Phone.named_formats[:short] = '%A/%n1-%n2' Phoner::Phone.named_formats[:short] = '%A/%n1-%n2'
pn.format(:short) # => 091/512-5486 pn.format(:short) # => 091/512-5486


= TODO = TODO
Expand All @@ -99,8 +105,11 @@ Currently tested on:
[US] United States [US] United States
[ZA] South Africa [ZA] South Africa


= Known issues
There's an issue with Germany and area codes.

= Author = Author
Copyright © 2010 Tomislav Car, Infinum Copyright © 2010 Tomislav Car, {Infinum}[http://www.infinumdigital.com]


= Contributors = Contributors
Don Morrison, Michael Squires, Todd Eichel (Fooala, Inc.), chipiga, Etienne Samson, Luke Randall Don Morrison, Michael Squires, Todd Eichel (Fooala, Inc.), chipiga, Etienne Samson, Luke Randall
Expand Down
30 changes: 30 additions & 0 deletions lib/country.rb
@@ -0,0 +1,30 @@
module Phoner
class Country < Struct.new(:name, :country_code, :char_2_code, :area_code)
cattr_accessor :all

def self.load
return @@all if @@all.present?

data_file = File.join(File.dirname(__FILE__), '..', 'data', 'phone_countries.yml')

@@all = {}
YAML.load(File.read(data_file)).each_pair do |key, c|
@@all[key] = Country.new(c[:name], c[:country_code], c[:char_2_code], c[:area_code])
end
@@all
end

def to_s
name
end

def self.find_by_country_code(code)
@@all[code]
end

def country_code_regexp
Regexp.new("^[+]#{country_code}")
end
end

end

0 comments on commit 4592f04

Please sign in to comment.