Skip to content

Commit

Permalink
Rewrite internals to use a unified interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
floere committed Jun 15, 2015
1 parent f4fef92 commit b6e7307
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 31 deletions.
30 changes: 17 additions & 13 deletions lib/phony/country.rb
Expand Up @@ -48,22 +48,26 @@ def with cc, options = {}
#
# Note: If the ndc is nil, it will not return it.
#
# @return [Trunk, String (ndc), Array<String> (national pieces)]
#
def split national_number
_, trunk, ndc, *rest = internal_split national_number
[trunk, ndc, *rest]
end
#
#
# @return [Splitters::Local, Trunk, String (ndc), Array<String> (national pieces)]
#
def internal_split national_number
trunk = nil
@codes.each do |code|
new_trunk, ndc, *rest = code.split national_number
@codes.each do |national_splitter|
new_trunk, ndc, *rest = national_splitter.split national_number
trunk ||= new_trunk
return [trunk, ndc, *rest] if rest && !rest.empty?
end
# Best effort in error case.
#
[trunk, national_number, []]
end
def split_ndc national_number
@codes.each do |code|
zero, ndc, *rest = code.split national_number
return [code.local_splitter, zero, ndc, *rest] if rest && !rest.empty?
return [national_splitter.local_splitter, trunk, ndc, *rest] if rest && !rest.empty?
end

# Best effort.
[nil, trunk, national_number, []]
end

# Format the number, given the national part of it.
Expand Down Expand Up @@ -163,7 +167,7 @@ def normalize national_number
# Tests for plausibility of this national number.
#
def plausible? rest, hints = {}
local, _, ndc, *rest = split_ndc rest
local, _, ndc, *rest = internal_split rest

# Element based checking.
#
Expand Down
23 changes: 10 additions & 13 deletions lib/phony/country_codes.rb
Expand Up @@ -73,16 +73,20 @@ def normalize number, options = {}
# Splits this number into cc, ndc and locally split number parts.
#
def split number
# TODO Think about reordering.
country, cc, trunk, *pieces = internal_split number
[cc, *pieces]
# Split the number into country, cc, and national part.
country, cc, national_number = partial_split number

# Split the national number into ndc and local part.
trunk, ndc, *local = country.split national_number

[cc, ndc, *local]
end

# Format the number.
#
def format number, options = {}
country, _, national = partial_split number
country.format national, options
country, _, national_number = partial_split number
country.format national_number, options
end
alias formatted format

Expand Down Expand Up @@ -130,14 +134,7 @@ def country_for number
country, _ = partial_split number
country
end

# Return a country and the split pieces of a number.
#
def internal_split number
country, cc, national = partial_split number
[country, cc, *country.split(national)]
end


# Split off the country and the cc, and also return the national number part.
#
def partial_split number
Expand Down
4 changes: 2 additions & 2 deletions lib/phony/national_splitters/default.rb
Expand Up @@ -9,7 +9,7 @@ class Default < DSL
def self.instance_for
@instance ||= new
end

# "Splits" the national part of a phone number into a single piece.
#
# @param [String] national_number An national part of a number.
Expand All @@ -20,7 +20,7 @@ def self.instance_for
# Phony.split("1234567") # => ["1234567"]
#
def split national_number
[national_number]
[nil, national_number]
end

# By default, the national part of a number is always plausible.
Expand Down
7 changes: 6 additions & 1 deletion qed/issues.md
Expand Up @@ -33,4 +33,9 @@ Italy numbers needed a 0 to be splittable (officially needed). Now they at least

### #221

Phony.assert.plausible?('+39 081 1925 2698')
Phony.assert.plausible?('+39 081 1925 2698')

### #259

Phony.split("4512121212").assert == ['45', false, '12', '12', '12', '12']
Phony.split("4234760987").assert == ['423', false, '476', '09', '87']
4 changes: 2 additions & 2 deletions spec/lib/phony/national_splitters/default_spec.rb
Expand Up @@ -12,8 +12,8 @@
let(:splitter) { described_class.instance_for }

describe 'split' do
it 'does not split' do
splitter.split(:anything).should == [:anything]
it 'does only pretend split' do
splitter.split(:anything).should == [nil, :anything]
end
end

Expand Down

0 comments on commit b6e7307

Please sign in to comment.