Skip to content

Commit

Permalink
+ Version 1.4.2: Various formats are settable.
Browse files Browse the repository at this point in the history
  • Loading branch information
floere committed May 6, 2011
1 parent 96bb9bb commit e62aa92
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 25 deletions.
4 changes: 4 additions & 0 deletions history.textile
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
h2. Version 1.4.2

* hanke: Exposing @Phony::CountryCodes.instance.international_absolute_format@, @Phony::CountryCodes.instance.international_relative_format@, @Phony::CountryCodes.instance.national_format@ (writer & reader) so people can set their special ndc formats. Especially if they use Phony for just one country. (Thanks sarwagya for the inspiration!)

h2. Version 1.4.1

* hanke: Moved @lib/countries.rb@ -> @lib/phony/countries.rb@ (Thanks trans!).
Expand Down
59 changes: 35 additions & 24 deletions lib/phony/country_codes.rb
Original file line number Diff line number Diff line change
@@ -1,55 +1,66 @@
module Phony


EMPTY_STRING = ''

# Handles determining the correct national code handler.
#
class CountryCodes

attr_reader :mapping


attr_reader :mapping
attr_accessor :international_absolute_format, :international_relative_format, :national_format

def initialize
@international_absolute_format = '+%s%s%s%s'
@international_relative_format = '00%s%s%s%s'
@national_format = '0%s%s'

@normalize_format = '%s%s'
@default_space = ' '
end

def self.instance
@instance ||= new
end

def normalize number
# Remove non-digit chars.
#
number.gsub! /\D*/, ''
number.gsub! /\D*/, EMPTY_STRING
national_handler, cc, rest = split_cc number
'%s%s' % [cc, national_handler.normalize(rest)]
@normalize_format % [cc, national_handler.normalize(rest)]
end

# Splits this number into cc, ndc and locally split number parts.
#
def split number
national_handler, cc, rest = split_cc number
[cc, *national_handler.split(rest)]
end

def formatted number, options = {}
format_cc_ndc_local options[:format], options[:spaces] || ' ', *split(number)
format_cc_ndc_local options[:format], options[:spaces] || @default_space, *split(number)
end
# Formats country code and national destination code.
#
def format_cc_ndc_local format, space, cc, ndc, *parts
"#{format_cc_ndc(format, space, cc, ndc)}#{format_local(space, parts)}"
end
def format_cc_ndc format, space, cc, ndc
format, split_phone_number = case format
case format
when nil, :international_absolute, :international, :+
[ndc.empty? ? '+%s%s' : '+%s%s%s%s', [cc, space, ndc, space]]
@international_absolute_format % [cc, space, ndc, space]
when :international_relative
[ndc.empty? ? '00%s%s' : '00%s%s%s%s', [cc, space, ndc, space]]
@international_relative_format % [cc, space, ndc, space]
when :national
[ndc.empty? ? '' : '0%s%s', [ndc, space]]
(ndc.empty? ? EMPTY_STRING : @national_format) % [ndc, space]
when :local
['', []]
EMPTY_STRING
end
format % split_phone_number
end
def format_local space, parts_ary
parts_ary.join space.to_s
end

#
#
def service? number
Expand All @@ -64,7 +75,7 @@ def landline? number
national_handler, cc, rest = split_cc number
national_handler.landline? rest
end

# Is the given number a vanity number?
#
def vanity? number
Expand All @@ -77,7 +88,7 @@ def vanity_to_number vanity_number
national_handler, cc, rest = split_cc vanity_number
"#{cc}#{national_handler.vanity_to_number(rest)}"
end

def split_cc rest
presumed_cc = ''
1.upto(3) do |i|
Expand All @@ -87,25 +98,25 @@ def split_cc rest
end
# This line is never reached as CCs are in prefix code.
end

# # TODO
# #
# def self.with_cc cc
# mapping[cc.size][cc.to_s]
# end

# Add the given country to the mapping under the
# given country code.
#
def add country_code, country
country_code = country_code.to_s
optimized_country_code_access = country_code.size

@mapping ||= {}
@mapping[optimized_country_code_access] ||= {}
@mapping[optimized_country_code_access][country_code] = country
end

end

end
2 changes: 1 addition & 1 deletion phony.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = 'phony'
s.version = '1.4.1'
s.version = '1.4.2'
s.authors = ['Florian Hanke']
s.email = 'florian.hanke+phony@gmail.com'
s.homepage = 'http://github.com/floere/phony'
Expand Down
40 changes: 40 additions & 0 deletions spec/lib/phony/country_codes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,46 @@
@countries = Phony::CountryCodes.instance
end

describe 'international_absolute_format=' do
it 'formats correctly' do
@countries.formatted('41443643532', :format => :international).should == '+41 44 364 35 32'
end
it 'formats correctly' do
old_format = @countries.international_absolute_format
@countries.international_absolute_format = '!!! %s%s%s%s'

@countries.formatted('41443643532', :format => :international).should == '!!! 41 44 364 35 32'

@countries.international_absolute_format = old_format
end
end
describe 'international_relative_format=' do
it 'formats correctly' do
@countries.formatted('41443643532', :format => :international_relative).should == '0041 44 364 35 32'
end
it 'formats correctly' do
old_format = @countries.international_relative_format
@countries.international_relative_format = '000 %s%s%s%s'

@countries.formatted('41443643532', :format => :international_relative).should == '000 41 44 364 35 32'

@countries.international_relative_format = old_format
end
end
describe 'national_format=' do
it 'formats correctly' do
@countries.formatted('41443643532', :format => :international_relative).should == '0041 44 364 35 32'
end
it 'formats correctly' do
old_format = @countries.national_format
@countries.national_format = '%s%s'

@countries.formatted('11231231234', :format => :national).should == '123 123 1234'

@countries.national_format = old_format
end
end

describe 'split' do
it 'splits correctly' do
@countries.split('41443643532').should == ['41', '44', '364', '35', '32']
Expand Down

0 comments on commit e62aa92

Please sign in to comment.