Permalink
Browse files

+ Rewrite to move all validators under class Validators

  • Loading branch information...
1 parent d1d3557 commit c1bc39b9bc9ab5cef1a65bf221140469f93c0306 @floere committed Mar 18, 2013
View
@@ -12,13 +12,12 @@
require File.expand_path '../phony/national_code', __FILE__
require File.expand_path '../phony/country', __FILE__
require File.expand_path '../phony/country_codes', __FILE__
-require File.expand_path '../phony/validator', __FILE__
require File.expand_path '../phony/validators', __FILE__
-#
# Validators
#
-require File.expand_path '../phony/validators/length_validator', __FILE__
+require File.expand_path '../phony/validators/ndc', __FILE__
+require File.expand_path '../phony/validators/length', __FILE__
require File.expand_path '../phony/dsl', __FILE__
View
@@ -177,7 +177,7 @@ def matched_split options = {}
# invalid_ndcs('911') # The regexp /911/ would also work.
#
def invalid_ndcs ndc
- Validator.new.ndc_check ndc
+ Validators::NDC.new.invalid ndc
end
# checks to see, if number is exactly length
@@ -194,7 +194,7 @@ def invalid_ndcs ndc
# length(8) # danish phone numbers are always 8 digits long
def length lengths
- LengthValidator.new lengths
+ Validators::Length.new lengths
end
end
View
@@ -1,26 +0,0 @@
-module Phony
-
- class Validator
-
- attr_reader :ndc_checks
-
- def initialize
- @ndc_checks = []
- end
-
- def plausible? ndc, rest
- ndc_checks && ndc_checks.each do |ndc_check|
- return false if ndc_check === ndc
- end
-
- true
- end
-
- def ndc_check ndc
- @ndc_checks << ndc
- self
- end
-
- end
-
-end
@@ -0,0 +1,32 @@
+module Phony
+
+ class Validators
+
+ class Length
+
+ # takes either a number (for fixed length phone numbers) or
+ # a hash where the key is the length of the ndc and the value the corresponding
+ # length
+ #
+ def initialize lengths
+ if lengths.is_a? Hash
+ @lengths = lengths
+ else
+ @lengths = Hash.new lengths
+ end
+ end
+
+ def plausible? ndc, rest
+ ndc ||= '' # handle 'false' ndc's (like Denmark)
+ lengths = [*@lengths[ndc.length]] # we can have one or more lengths, make them into an array
+
+ length = ndc.size + rest.inject(0) { |sum, part| part ? sum + part.size : sum }
+
+ return false unless lengths.include? length
+ true
+ end
+
+ end
+ end
+end
+
@@ -1,27 +0,0 @@
-module Phony
- class LengthValidator
-
- # takes either a number (for fixed length phone numbers) or
- # a hash where the key is the length of the ndc and the value the corresponding
- # length
- def initialize lengths
- if lengths.is_a? Hash
- @lengths = lengths
- else
- @lengths = Hash.new lengths
- end
- end
-
- def plausible? ndc, rest
- ndc ||= '' # handle 'false' ndc's (like Denmark)
- lengths = [*@lengths[ndc.length]] # we can have one or more lengths, make them into an array
-
- length = ndc.size + rest.inject(0) { |sum, part| part ? sum + part.size : sum }
-
- return false unless lengths.include? length
- true
- end
-
- end
-end
-
@@ -0,0 +1,30 @@
+module Phony
+
+ class Validators
+
+ class NDC
+
+ attr_reader :invalids
+
+ def initialize
+ @invalids = []
+ end
+
+ def plausible? ndc, rest
+ @invalids && @invalids.each do |invalid|
+ return false if invalid === ndc
+ end
+
+ true
+ end
+
+ def invalid ndc
+ @invalids << ndc
+ self
+ end
+
+ end
+
+ end
+
+end

0 comments on commit c1bc39b

Please sign in to comment.