Created
October 18, 2016 13:24
-
-
Save intrip/bb3c7e30062609ee1dc33277ae3efbae to your computer and use it in GitHub Desktop.
Italian fiscal code generator for Ruby on Rails
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Italian | |
# Generates a random fiscal code | |
class << self | |
@@generated_cfs = [] | |
def cf | |
res = [] | |
# ^[A-Z]{6} | |
res << _A_Z(6) | |
# [0-9]{2} | |
res << _0_9(2) | |
# [ABCDEHLMPRST] | |
res << Array.new(1).map { ['ABCDEHLMPRST'].split.shuffle.first } | |
#[0-9]{2} | |
res << _0_9(2) | |
# [A-Z] | |
res << _A_Z(1) | |
# [0-9]{3} | |
res << _0_9(3) | |
# [A-Z]$ | |
res << _A_Z(1) | |
res.join | |
end | |
# Generates a random unique fiscal code | |
def cf_uniq | |
random_cf = cf | |
while(@@generated_cfs.include?(random_cf)) | |
random_cf = cf | |
end | |
@@generated_cfs << random_cf | |
random_cf | |
end | |
private | |
def _A_Z(length) | |
Array.new(length).map { ('A'..'Z').to_a[rand(26)] } | |
end | |
def _0_9(length) | |
Array.new(length).map { rand(10)} | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment