Permalink
Browse files

+ intermediate commit

  • Loading branch information...
1 parent f3495b3 commit 40e77a85a527fb7dcca759da6536f3790d18b55e @floere committed Nov 19, 2010
View
@@ -13,6 +13,8 @@
require File.expand_path '../phony/national_splitters/fixed', __FILE__
require File.expand_path '../phony/national_splitters/variable', __FILE__
require File.expand_path '../phony/national_code', __FILE__
+require File.expand_path '../phony/special_code', __FILE__
+require File.expand_path '../phony/country', __FILE__
# Countries.
#
@@ -7,20 +7,8 @@ class All
# Returns an anonymous fixed size ndc / format country handler.
#
def self.fixed size, options = {}
- local_format = options[:local_format] || [3, 2, 2] # Default is the 3-2-2 format.
-
- # If service ndsc, handle through non fixed national code splitter.
- #
- service_ndcs = options[:service_ndcs]
- mobile_ndcs = options[:mobile_ndcs]
- national_splitter = if service_ndcs || mobile_ndcs
- NationalSplitters::Variable.new size,
- :service => [*service_ndcs],
- :mobile => [*mobile_ndcs]
- else
- NationalSplitters::Fixed.instance_for size
- end
- local_splitter = LocalSplitter.instance_for local_format
+ national_splitter = NationalSplitters::Fixed.instance_for size
+ local_splitter = LocalSplitter.instance_for options[:local_format] # Default is the 3-2-2 format.
NationalCode.new national_splitter, local_splitter
end
@@ -46,8 +34,10 @@ def self.mapping
'32' => fixed(2), # Belgium
'33' => fixed(1,
:local_format => [2, 2, 2, 2],
- :service_ndcs => %w{8},
- :mobile_ndcs => %w{6 7}
+ :special_ndcs => {
+ :service => %w{8},
+ :mobile => %w{6 7}
+ }
), # France
'34' => fixed(2), # Spain
'36' => fixed(2), # Hungary
@@ -56,8 +46,10 @@ def self.mapping
'40' => fixed(2), # Romania
'41' => fixed(2,
:local_format => [3, 2, 2],
- :service_ndcs => %w{800 840 842 844 848},
- :mobile_ndcs => %w{74 76 77 78 79}
+ :special_ndcs => {
+ :service => %w{800 840 842 844 848},
+ :mobile => %w{74 76 77 78 79}
+ }
), # Switzerland (Confederation of)
'43' => Countries::Austria.new,
'44' => fixed(2), # United Kingdom of Great Britain and Northern Ireland
@@ -1,19 +1,34 @@
+# Phony::Countries::Austria = Phony::Country.configured :local_format => [10],
+# :local_special_format => [10],
+# :ndc_fallback_length => 4,
+# :ndc_mapping => {
+# :normal => [
+# '1'
+# ] # etc.
+# }
+# # :ndc_length
+
module Phony
module Countries
# Numbers for Austria.
#
- class Austria < Phony::NationalCode
+ class Austria < Phony::Country
def initialize
national_splitter = NationalSplitters::Variable.new 4, @@ndc_mapping
local_splitter = LocalSplitter.instance_for [10]
+ national_code = NationalCode.new national_splitter, local_splitter
- super national_splitter, local_splitter
+ national_special_splitter = NationalSplitters::Variable.new 4, @@ndc_mapping
+ local_special_splitter = LocalSplitter.instance_for [10]
+ national_special_code = SpecialCode.new national_special_splitter, local_special_splitter
+
+ super national_code, national_special_code
end
@@ndc_mapping = {
- :landline => [
+ :normal => [
'1', # Vienna
'57', # -
'59', # -
View
@@ -0,0 +1,78 @@
+module Phony
+
+ #
+ #
+ class Country
+
+ def initialize national_code, special_code = nil
+ @national_code = national_code
+ @special_code = special_code
+ end
+
+ #
+ #
+ def split national_number
+ ndc, *rest = @special_code.split national_number if @special_code
+ ndc, *rest = @national_code.split national_number unless rest && !rest.empty?
+ [ndc, *rest]
+ end
+
+ # Get a configured country instance.
+ #
+ # Define your countries like this:
+ # Phony::Countries::Austria = Phony::Country.configured <options>
+ #
+ # Example:
+ # Phony::Country.configured :local_format => [3, 2, 2],
+ # :special_local_format => [3, 3],
+ # :ndc_fallback_length => 4,
+ # :ndc_mapping => {
+ # :normal => ['44'],
+ # :service => ['800'],
+ # :mobile => ['76']
+ # }
+ #
+ def self.configured options = {}
+ ndc_fallback_length = options[:ndc_fallback_length]
+ ndc_mapping = options[:ndc_mapping]
+
+ # TODO Extract ndcs.
+ #
+
+ national_splitter = Phony::NationalSplitters::Variable.new ndc_fallback_length, ndc_mapping
+ local_splitter = Phony::LocalSplitter.instance_for options[:local_format] || [3, 2, 2]
+ national_code = Phony::NationalCode.new national_splitter, local_splitter
+
+ special_national_splitter = Phony::NationalSplitters::Variable.new nil, ndc_mapping
+ special_local_splitter = Phony::LocalSplitter.instance_for local_special_format || [3, 3]
+ special_code = Phony::SpecialCode.new special_national_splitter, special_local_splitter
+
+ new national_code, special_code
+ end
+
+ # Gets a configured country instance with fixed length ndc code.
+ #
+ # Define your countries like this:
+ # Phony::Countries::Switzerland = Phony::Country.fixed <options>
+ #
+ def self.configured options = {}
+ ndc_fallback_length = options[:ndc_fallback_length]
+ ndc_mapping = options[:ndc_mapping]
+
+ # TODO Extract ndcs.
+ #
+
+ national_splitter = Phony::NationalSplitters::Variable.new ndc_fallback_length, ndc_mapping
+ local_splitter = Phony::LocalSplitter.instance_for options[:local_format] || [3, 2, 2]
+ national_code = Phony::NationalCode.new national_splitter, local_splitter
+
+ special_national_splitter = Phony::NationalSplitters::Variable.new nil, ndc_mapping
+ special_local_splitter = Phony::LocalSplitter.instance_for local_special_format || [3, 3]
+ special_code = Phony::SpecialCode.new special_national_splitter, special_local_splitter
+
+ new national_code, special_code
+ end
+
+ end
+
+end
@@ -16,12 +16,16 @@ class LocalSplitter
#
# Caches the created splitter for the given format.
#
- def self.instance_for format
+ def self.instance_for format = nil
@mapping[format] ||= new(format)
end
- def initialize format
- @format = format
+ # Initialize with a local format, like [3, 2, 2] (also the default).
+ #
+ # The format [3, 2, 2] splits a number like '3332222' into ['333', '22', '22'].
+ #
+ def initialize format = nil
+ @format = format || [3, 2, 2]
end
# Split a local number according to an assumed country specific format.
@@ -2,11 +2,11 @@ module Phony
# This is the superclass of all special national number handlers.
#
- # NationalCodes have a NationalCodeSplitter and a LocalCodeSplitter.
+ # NationalCodes have a special numbers splitter, a national code splitter and a local code splitter.
#
class NationalCode
- # ndcs = {}, local_format = [], service_ndcs = []
+ #
#
def initialize national_splitter, local_splitter
@national_splitter = national_splitter
@@ -17,20 +17,11 @@ def initialize national_splitter, local_splitter
# its parts.
#
def split national_number
- ndc, rest = @national_splitter.split national_number
+ ndc, rest = @national_splitter.split national_number.dup
+ return ndc unless rest
[ndc, *@local_splitter.split(rest)]
end
- def service? national_number
- @national_splitter.service? national_number
- end
- def mobile? national_number
- @national_splitter.mobile? national_number
- end
- def landline? national_number
- !mobile?(ndc) && !service?(ndc)
- end
-
# Is this national number a vanity number?
#
def vanity? national_number
@@ -2,9 +2,11 @@ module Phony
module NationalSplitters
+ # TODO
+ #
class Fixed
- attr_writer :service_ndcs, :mobile_ndcs
+ attr_writer :special_splitter
@mapping = {}
@@ -18,30 +20,15 @@ def self.instance_for size
def initialize size
@size = size
- @service_ndcs = []
- @mobile_ndcs = []
end
# Takes a national number and splits it into ndc and rest.
#
def split national_number
+ return national_number unless @size
[national_number.slice!(0...@size), national_number]
end
-
- def service? number
- ndc, _ = split number
- @service_ndcs.include? ndc
- end
- def mobile? number
- ndc, _ = split number
- @mobile_ndcs.include? ndc
- end
- def landline? number
- ndc, _ = split number
- !mobile?(ndc) && !service?(ndc)
- end
-
end
end
@@ -8,7 +8,7 @@ class Variable < Fixed
#
def initialize fallback, ndc_map
super fallback
- @ndcs, @special_ndcs = restructure ndc_map
+ @ndcs = restructure ndc_map
end
# Takes a national number and splits it into ndc and rest.
@@ -18,48 +18,29 @@ def split national_number
# Extract a starting point.
#
- presumed_code = if @mapped_ndc_min_length > 1
- national_number.slice!(0..@mapped_ndc_min_length-2)
+ presumed_code = if @mapped_ndc_min_length > 0
+ national_number.slice!(0..@mapped_ndc_min_length-1)
else
''
end
# Try for all possible mapped.
#
@mapped_ndc_min_length.upto(@mapped_ndc_max_length) do |i|
- presumed_code << national_number.slice!(0..0)
sized_ndcs = @ndcs[i]
return [presumed_code, national_number] unless sized_ndcs && !sized_ndcs.include?(presumed_code)
+ presumed_code << national_number.slice!(0..0)
end
# Not found.
#
return super(fallback_number)
end
- def service? number
- ndc, _ = split number
- @special_ndcs[:service].include? ndc
- end
- def mobile? number
- ndc, _ = split number
- @special_ndcs[:mobile].include? ndc
- end
- def landline? number
- ndc, _ = split number
- !mobile?(ndc) && !service?(ndc)
- end
-
private
def restructure ndc_map
- [optimize(ndc_map.values.flatten), extract_special(ndc_map)] # TODO extract_services ndc_map
- end
-
- def extract_special ndc_map
- ndc_map.each_pair do |type, ndc|
-
- end
+ optimize ndc_map.values.flatten
end
# Optimizes and restructures the given ndcs array.
Oops, something went wrong.

0 comments on commit 40e77a8

Please sign in to comment.