Module: Cuid
- Defined in:
- lib/cuid.rb
Overview
Cuid is a library for generating unique collision-resistant IDs optimized for horizontal scaling and performance
Constant Summary collapse
- BLOCK_SIZE =
length of each segment of the hash
4
- BASE =
size of the alphabet (e.g. base36 is [a-z0-9])
36
- RAND_SIZE =
size of the random segment of the block
BLOCK_SIZE * 2
Class Method Summary collapse
-
.generate(quantity = 1, secure_random = false) ⇒ Object
Returns one or more hashes based on the parameter supplied.
-
.validate(str) ⇒ Boolean
Validates (minimally) the supplied string is in the correct format to be a hash.
Class Method Details
.generate ⇒ String .generate(quantity) ⇒ Array<String> .generate(quantity, secure_random) ⇒ Array<String>
Returns one or more hashes based on the parameter supplied
79 80 81 82 83 84 85 86 |
# File 'lib/cuid.rb', line 79 def generate(quantity=1,secure_random=false) @use_secure_random = secure_random @fingerprint = get_fingerprint # only need to get the fingerprint once because it is constant per-run return api unless quantity > 1 values = Array(1.upto(quantity)) # create an array of the correct size return values.collect { api } # fill array with hashes end |
.validate(str) ⇒ Boolean
Validates (minimally) the supplied string is in the correct format to be a hash
Validation checks that the first letter is correct and that the rest of the string is the correct length and consists of lower case letters and numbers.
96 97 98 99 |
# File 'lib/cuid.rb', line 96 def validate(str) blen = BLOCK_SIZE * 6 !!str.match(/#{LETTER}[a-z0-9]{#{blen}}/) end |