Skip to content

Instantly share code, notes, and snippets.

@zmalltalker
Created October 23, 2008 10:53
Show Gist options
  • Save zmalltalker/18982 to your computer and use it in GitHub Desktop.
Save zmalltalker/18982 to your computer and use it in GitHub Desktop.
module Modulus11
def modulus_11_ok?
sum = 0
weights = [5,4,3,2,7,6,5,4,3,2]
digits = cdv_string.split(//)
10.times do |n|
num = digits[n].to_i
sum += num.to_i * weights[n].to_i
end
rest = sum % 11
control_digit = case rest
when 0:
0
when 1
nil
else
11 - rest
end
match = cdv_string.split(//)[-1]
result = match == control_digit.to_s
# puts "Comparing #{cdv_string}: expected #{control_digit} with #{match}, giving #{result}"
return result
end
end
module Modulus10
def modulus_10_ok?
sum = 0
weights = [3,7,6,1,8,9,4,5,2]
digits = cdv_string.split(//)
9.times do |n|
num = digits[n].to_i
sum += num.to_i * weights[n].to_i
end
rest = sum % 11
control_digit = 11 - rest
match = cdv_string.split(//)[9]
result = control_digit.to_s == match
# puts "Control digit for #{cdv_string} is #{control_digit}. Comparing to #{match}. Returning #{result}"
end
end
class LegalEntity
include Modulus11
include Modulus10
attr_reader :organization_number
def initialize(o)
@organization_number = o
end
def cdv_string
sprintf("%011d", @organization_number.to_i)
end
def person?
@organization_number.length == 11
end
def organization?
@organization_number.length == 9
end
def unknown_entity?
return @organization_number == "999999999"
end
def valid?
begin
if organization?
result = modulus_11_ok?
else
result = modulus_11_ok? && modulus_10_ok?
end
rescue
result = false
end
return result
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment