Skip to content

Commit

Permalink
1..7-digit ING numbers now possible, closes #1
Browse files Browse the repository at this point in the history
9..10-digit number now possible, closes #2
  • Loading branch information
Sytze Loor committed Mar 13, 2012
1 parent 4de0e9b commit 0cd13fb
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 12 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ and run
Simply add "validates_elfproef_of" to your model class.

class User < ActiveRecord::Base
# Validate with 11-test, do not allow 7 digit ING account numbers
# Validate with 11-test, do not allow 7 digit ING account numbers
validates :bsn, elfproef: true

# Validate bank account numbers with 11-test, allow 7 digit ING account numbers
# Validate bank account numbers with 11-test, allow 7 digit ING account numbers
validates :bank_account, elfproef: { allow_ing: true }
end

Expand Down
8 changes: 4 additions & 4 deletions lib/elfproef.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ module Elfproef
# http://nl.wikipedia.org/wiki/Elfproef
def self.elfproef(number, options = {})
number, sum = number.to_s, 0
number.gsub!(/\D/, '') # strip out any non-digit character.
return true if options[:allow_ing] && number.length == 7 # always pass postbank accounts (7 digits)
return false unless number =~ /^\d{9}$/ # account should be exactly 9 digits
(1..9).each do |c|
number.gsub!(/\D/, '') # strip out any non-digit character.
return true if options[:allow_ing] && (1..7).include?(number.length) # always pass ING accounts (between 1 and 7 digits)
return false unless (9..10).include?(number.length) # account should be exactly 9 or 10 digits
(1..number.length).each do |c|
sum += number[-c].chr.to_i * c
end
sum % 11 == 0
Expand Down
2 changes: 1 addition & 1 deletion lib/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Elfproef
VERSION= "0.0.2"
VERSION= "0.0.3"
end
54 changes: 49 additions & 5 deletions spec/elfproef_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
Elfproef.elfproef('1234567', opts).should == true
end


shared_examples_for "elfproef validations" do
subject { account_class.new }

Expand All @@ -50,21 +49,61 @@
end

it "fails with an obviously wrong number" do
subject.account = "666"
subject.account = "12345678901"
subject.should_not be_valid
subject.errors[:account].should == errors
end

it "passes with a ING number" do
it "passes with a 7-diget ING number" do
subject.account = "1234567"
subject.should be_valid
end

it "passes with a valid bank number" do
it "passes with a 6-digit ING number" do
subject.account = "123456"
subject.should be_valid
end

it "passes with a 5-digit ING number" do
subject.account = "12345"
subject.should be_valid
end

it "passes with a 4-digit ING number" do
subject.account = "1234"
subject.should be_valid
end

it "passes with a 3-digit ING number" do
subject.account = "123"
subject.should be_valid
end

it "passes with a 2-digit ING number" do
subject.account = "12"
subject.should be_valid
end

it "passes with a 1-digit ING number" do
subject.account = "1"
subject.should be_valid
end

it "passes with a ING number starting with a P (from Postbank)" do
subject.account = "P1234567"
subject.should be_valid
end

it "passes with a valid 9-digit bank number" do
subject.account = "123456789"
subject.should be_valid
end

it "passes with a valid 10-digit bank number" do
subject.account = "0123456789"
subject.should be_valid
end

it "fails with an wrong bank number" do
subject.account = "999999999"
subject.should_not be_valid
Expand Down Expand Up @@ -92,11 +131,16 @@
subject.errors[:account].should == errors
end

it "passes with a valid bank number" do
it "passes with a valid 9-digit bank number" do
subject.account = "123456789"
subject.should be_valid
end

it "passes with a valid 10-digit bank number" do
subject.account = "0123456789"
subject.should be_valid
end

it "fails with an wrong bank number" do
subject.account = "999999999"
subject.should_not be_valid
Expand Down

0 comments on commit 0cd13fb

Please sign in to comment.