Permalink
Browse files

+ Mid-rewrite

  • Loading branch information...
1 parent e763846 commit 8836b366cb2de3756887fd0237682d78d892f970 @floere committed Mar 20, 2013
View
15 lib/phony/country.rb
@@ -29,6 +29,12 @@ def split national_number
return [zero, ndc, *rest] if rest && !rest.empty?
end
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?
+ end
+ end
# Removes 0s from partially normalized numbers
# such as 410443643533.
@@ -46,6 +52,15 @@ def normalize national_number
return nil
end
+ #
+ #
+ def plausible? ndc, rest
+ @codes.each do |code|
+ zero, ndc, *rest = code.split national_number
+ return [zero, ndc, *rest] if rest && !rest.empty?
+ end
+ end
+
# Is this national number a vanity number?
#
def vanity? national_number
View
11 lib/phony/dsl.rb
@@ -49,9 +49,13 @@ class DSL
# Example:
# country '27', # CC, followed by rules, for example fixed(2) >> ...
#
- def country country_code, country, *validators
- Phony::CountryCodes.instance.add country_code, country
+ def country country_code, definition, *validators
+ Phony::CountryCodes.instance.add country_code, definition
validators.each do |validator|
+ # codes = definition.codes
+ # codes.each do |code|
+ # p [code.instance_variable_get(:@national_splitter).length, code.instance_variable_get(:@local_splitter).length]
+ # end
Phony::Validators.instance.add country_code, validator
end
end
@@ -125,7 +129,6 @@ def match regex, options = {}
#
raise "Regexp /#{regex.source}/ needs a group in it that defines which digits belong to the NDC." unless regex.source =~ /\(/
- on_fail_take = options.delete :on_fail_take
NationalSplitters::Regex.instance_for regex, options[:on_fail_take], options
end
@@ -199,4 +202,4 @@ def length lengths
end
-end
+end
View
6 lib/phony/local_splitters/fixed.rb
@@ -43,6 +43,12 @@ def split number
end
end
+ # A valid length.
+ #
+ def length
+ @length ||= @format.inject { |total, part| total + part }
+ end
+
end
end
View
6 lib/phony/local_splitters/regex.rb
@@ -42,6 +42,12 @@ def split number
split_with number, fallback
end
+ # A valid length.
+ #
+ def length
+ # raise "#{self.class.name} has no length that can be automatically extracted."
+ end
+
private
def split_with number, format
View
4 lib/phony/national_code.rb
@@ -3,7 +3,9 @@ module Phony
# NationalCodes have a special numbers splitter, a national code splitter and a local code splitter.
#
class NationalCode
-
+
+ attr_reader :local_splitter
+
#
#
def initialize national_splitter, local_splitter, normalize = nil, trunk_code = '0'
View
6 lib/phony/national_splitters/default.rb
@@ -11,6 +11,12 @@ def self.instance_for
def split national_number
[national_number]
end
+
+ # A valid length.
+ #
+ def length
+ 1
+ end
end
end
View
6 lib/phony/national_splitters/fixed.rb
@@ -30,6 +30,12 @@ def split national_number
[@zero, national_number.slice!(0...@size), national_number]
end
+ # A valid length.
+ #
+ def length
+ @size
+ end
+
end
end
View
6 lib/phony/national_splitters/none.rb
@@ -39,6 +39,12 @@ def split national_number
[nil, false, national_number]
end
+ # A valid length.
+ #
+ def length
+ 0
+ end
+
end
end
View
6 lib/phony/national_splitters/regex.rb
@@ -42,6 +42,12 @@ def split national_number
super national_number
end
+ # A valid length.
+ #
+ def length
+ # raise "#{self.class.name} has no length that can be automatically extracted."
+ end
+
end
end
View
8 lib/phony/national_splitters/variable.rb
@@ -37,6 +37,12 @@ def split national_number
super fallback_number
end
+ # A valid length.
+ #
+ def length
+ (@mapped_ndc_min_length..@mapped_ndc_max_length)
+ end
+
private
# def restructure ndc_map
@@ -45,7 +51,7 @@ def split national_number
# Optimizes and restructures the given ndcs array.
#
- def optimize(ndc_ary)
+ def optimize ndc_ary
ndcs = {}
ndc_ary.each do |ndc|
ndcs[ndc.length] ||= []
View
19 lib/phony/validators.rb
@@ -47,7 +47,8 @@ def plausible? number, hints = {}
# Hint based checking.
#
- cc, ndc, *rest = Phony.split normalized
+ country, cc, rest = CountryCodes.instance.split_cc normalized
+ local, _, ndc, _ = country.split_ndc rest
# Element based checking.
#
@@ -56,9 +57,11 @@ def plausible? number, hints = {}
return false if ndc.nil?
return false if ndc && ndc.empty?
+ rest_size = ndc ? rest.size - ndc.size : rest.size
+
# A valid range for the rest is 0 or 3+ total digits.
#
- return false if (1..2) === rest.reduce(0) { |total, string| total + string.size }
+ return false if (1..2) === rest_size
# CC.
#
@@ -72,12 +75,14 @@ def plausible? number, hints = {}
# Country specific checks.
#
- validators_for(cc).each do |validator|
- return false unless validator.plausible? ndc, rest
- end
+ # validators_for(cc).each do |validator|
+ # return false unless validator.plausible? ndc, rest
+ # end
+ return false unless (local.length-10) === rest_size
true
- rescue StandardError
+ rescue StandardError => e
+ puts e.message
return false
end
@@ -86,7 +91,7 @@ def validators_for cc
end
def default_validators
- @default_validators ||= [Validator.new]
+ @default_validators ||= []
end
end
View
1 lib/phony/validators/length.rb
@@ -29,4 +29,3 @@ def plausible? ndc, rest
end
end
end
-

0 comments on commit 8836b36

Please sign in to comment.