Permalink
Browse files

14 digit regon validation

  • Loading branch information...
1 parent e9ef8e7 commit 9852194b6b6b3e889176d1dde866bdee651a17f2 @akwiatkowski akwiatkowski committed Nov 28, 2011
Showing with 38 additions and 4 deletions.
  1. +3 −1 lib/valideez/common.rb
  2. +11 −3 lib/valideez/regon.rb
  3. +12 −0 spec/regon_spec.rb
  4. +12 −0 spec/regon_validator_spec.rb
View
@@ -5,7 +5,9 @@ def validate_format
end
def validate_length
- length == val.size
+ # length can be a Fixnum or Array of Fixnums
+ lengths = length.kind_of?(Array) ? length : [length]
+ lengths.include? val.size
end
end
View
@@ -6,16 +6,22 @@ def initialize(val, options = {})
super val
assign({
- :format => /\A\d{9}\Z/,
- :length => 9,
+ #:format => /\A\d{9}\d{5}*\Z/,
+ :format => /\A(\d{9}|\d{14})\Z/,
+ :length => [9, 14],
:mask => [8, 9, 2, 3, 4, 5, 6, 7],
+ :mask_long => [2, 4, 8, 5, 0, 9, 7, 3, 6, 1, 2, 4, 8],
:modulo => 11,
:sum_control => true,
}.merge(options))
@arr = []
end
+ def is_long?
+ val.size === 14
+ end
+
def validate_sum_control
mod = checksum % modulo
mod === @arr.shift
@@ -25,7 +31,9 @@ def validate_sum_control
def checksum
@arr = val.split("").collect(&:to_i)
- mask.inject(0) {|sum, w| sum + w * @arr.shift}
+ # there is the other mask for 14 digit regon
+ m = is_long? ? mask_long : mask
+ m.inject(0) {|sum, w| sum + w * @arr.shift}
end
end
View
@@ -7,9 +7,21 @@
end
end
+ it "should be valid 14-digit regon" do
+ %w[12345678512347].each do |n|
+ Valideez::Regon.new(n).should be_valid
+ end
+ end
+
it "should be invalid" do
%w[590096453 59009645412 390096454 ag5435 00000000].each do |n|
Valideez::Regon.new(n).should_not be_valid
end
end
+
+ it "should be invalid 14-digit regon" do
+ %w[12345678512348 12345678512346 1234567851237].each do |n|
+ Valideez::Regon.new(n).should_not be_valid
+ end
+ end
end
@@ -25,4 +25,16 @@ class RegonBastard
@model.regon = '590096453'
@model.should_not be_valid
end
+
+ it "should be valid 14-digit regon" do
+ @model.should_not be_valid
+ @model.regon = '12345678512347'
+ @model.should be_valid
+ end
+
+ it "should be invalid 14-digit regon" do
+ @model.should_not be_valid
+ @model.regon = '12345678512348'
+ @model.should_not be_valid
+ end
end

0 comments on commit 9852194

Please sign in to comment.