Module: AegisSupport::SecureNumber::ClassMethods
- Defined in:
- lib/aegis_support/secure_number.rb
Constant Summary collapse
- DEFAULT_MAXIMUM_NUMBER_LENGTH =
20
Instance Method Summary collapse
- #generate_secure_number(attribute, **options) ⇒ Object
- #has_secure_number(attribute, **options) ⇒ Object
Instance Method Details
#generate_secure_number(attribute, **options) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/aegis_support/secure_number.rb', line 15 def generate_secure_number(attribute, **) .symbolize_keys! 10.times do |i| secure_number = [:prefix].to_s + random_string([:length]) + [:suffix].to_s if exists?(attribute => secure_number) raise "Couldn't generate a unique number in 10 attempts!" if i == 9 else break secure_number end end end |
#has_secure_number(attribute, **options) ⇒ Object
8 9 10 11 12 13 |
# File 'lib/aegis_support/secure_number.rb', line 8 def has_secure_number(attribute, **) require "securerandom" define_method("regenerate_#{attribute}") { update! attribute => self.class.generate_secure_number(attribute, **) } before_create { self.send("#{attribute}=", self.class.generate_secure_number(attribute, **)) } end |