Skip to content

Instantly share code, notes, and snippets.

@intrip
Created October 18, 2016 13:24
Show Gist options
  • Save intrip/bb3c7e30062609ee1dc33277ae3efbae to your computer and use it in GitHub Desktop.
Save intrip/bb3c7e30062609ee1dc33277ae3efbae to your computer and use it in GitHub Desktop.
Italian fiscal code generator for Ruby on Rails
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