Skip to content

Commit

Permalink
Allow unsupported countries to use a more conservative default.
Browse files Browse the repository at this point in the history
  • Loading branch information
mrchucho committed Jul 15, 2011
1 parent e62aa92 commit 3851473
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 7 deletions.
3 changes: 2 additions & 1 deletion lib/phony.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
require File.expand_path '../phony/national_splitters/variable', __FILE__
require File.expand_path '../phony/national_splitters/regex', __FILE__
require File.expand_path '../phony/national_splitters/none', __FILE__
require File.expand_path '../phony/national_splitters/default', __FILE__
require File.expand_path '../phony/national_code', __FILE__
require File.expand_path '../phony/country', __FILE__
require File.expand_path '../phony/country_codes', __FILE__
Expand Down Expand Up @@ -96,4 +97,4 @@ def vanity_to_number vanity_number

end

end
end
4 changes: 2 additions & 2 deletions lib/phony/countries.rb
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@
country '878', fixed(2) >> split(3,2,2) # Universal Personal Telecommunication Service (UPT)
country '879', fixed(2) >> split(3,2,2) # Reserved for national non-commercial purposes

country '880', fixed(2) >> split(3,2,2) # Bangladesh (People's Republic of)
country '880', default # Bangladesh (People's Republic of)
country '881', fixed(2) >> split(3,2,2) # International Mobile, shared code
country '882', fixed(2) >> split(3,2,2) # International Networks, shared code
country '883', fixed(2) >> split(3,2,2) # -
Expand Down Expand Up @@ -515,4 +515,4 @@
country '997', fixed(2) >> split(3,2,2) # Spare code
country '998', fixed(2) >> split(3,2,2) # Uzbekistan (Republic of)
country '999', fixed(2) >> split(3,2,2) # Reserved for possible future use within the Telecommunications for Disaster Relief (TDR) concept
end
end
12 changes: 10 additions & 2 deletions lib/phony/country_codes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,15 @@ def formatted number, options = {}
# 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)}"
cc_ndc = format_cc_ndc(format, space, cc, ndc)
local = \
if parts.empty?
cc_ndc = cc_ndc.slice(0...cc_ndc.size-1)
EMPTY_STRING
else
format_local(space, parts) unless parts.empty?
end
"#{cc_ndc}#{local}"
end
def format_cc_ndc format, space, cc, ndc
case format
Expand Down Expand Up @@ -119,4 +127,4 @@ def add country_code, country

end

end
end
5 changes: 4 additions & 1 deletion lib/phony/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ def one_of *ndcs
def match regex, options = {}
NationalSplitters::Regex.instance_for regex, options[:on_fail_take]
end
def default
NationalSplitters::Default.instance_for
end

# Local splitters.
#
Expand All @@ -88,4 +91,4 @@ def matched_split options = {}

end

end
end
17 changes: 17 additions & 0 deletions lib/phony/national_splitters/default.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Phony

module NationalSplitters

class Default < DSL

def self.instance_for
new
end

def split number
number
end

end
end
end
13 changes: 12 additions & 1 deletion spec/lib/phony_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,17 @@
Phony.format('4233841148', :format => :international_relative, :spaces => :-).should == '00423-384-11-48'
end
end
describe '"unsupported" countries' do
it 'should format as a single block' do
Phony.format('88032155605220').should == '+880 32155605220'
end
it 'should format as a single block, regardless of format' do
Phony.format('8801819372205', :format => :international).should == '+880 1819372205'
end
it 'should format as a single block, respecting custom spaces' do
Phony.format('8801819372205', :spaces => :-).should == '+880-1819372205'
end
end
end
describe "national" do
it "should format swiss numbers" do
Expand Down Expand Up @@ -238,4 +249,4 @@
end
end

end
end

0 comments on commit 3851473

Please sign in to comment.