Module: UniqueGenerator::ClassMethods

Defined in:
lib/unique_generator.rb

Instance Method Summary collapse

Instance Method Details

#generate_random(length = 32) ⇒ Object

Generate a random string with the given number of characters.



58
59
60
# File 'lib/unique_generator.rb', line 58

def generate_random(length = 32)
  (1..length).inject("") { |token, _| token << CHOICES.sample }
end

#generate_unique(length = 32, &blk) ⇒ Object

Generate a unique field



51
52
53
54
55
# File 'lib/unique_generator.rb', line 51

def generate_unique(length = 32, &blk)
  unique = generate_random(length)
  unique = generate_random(length) until blk.call(unique)
  unique
end