Module: Cuid 67 | 68 | 69 | 70 |
71 | 72 |-
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
- Defined in: 82 |
- lib/cuid.rb 83 | 84 |
Overview
Cuid is a library for generating unique collision-resistant IDs optimized for horizontal scaling and performance
90 | 91 | 92 |Constant Summary
123 | 124 |-
125 |
126 |
- BLOCK_SIZE =
127 | 128 |134 | 138 |129 |133 |
length of each segment of the hash
130 | 131 | 132 |
139 | 4
140 |
141 | - BASE =
142 | 143 |149 | 153 |144 |148 |
size of the alphabet (e.g. base36 is [a-z0-9])
145 | 146 | 147 |
154 | 36
155 |
156 | - RAND_SIZE =
157 | 158 |164 | 168 |159 |163 |
size of the random segment of the block
160 | 161 | 162 |
169 | BLOCK_SIZE * 2
170 |
171 |
182 | Class Method Summary 183 | (collapse) 184 |
185 | 186 |-
187 |
188 |
-
189 |
190 |
191 | + (Object) generate(quantity = 1, secure_random = false)
192 |
193 |
194 |
195 |
196 |
197 |
198 |
199 |
200 |
201 |
202 |
203 |
204 |
205 | 207 | 208 |
Returns one or more hashes based on the parameter supplied.
206 |
209 |
210 |
211 | -
212 |
213 |
214 | + (Boolean) validate(str)
215 |
216 |
217 |
218 |
219 |
220 |
221 |
222 |
223 |
224 |
225 |
226 |
227 |
228 | 230 | 231 |
Validates (minimally) the supplied string is in the correct format to be a hash.
229 |
232 |
233 |
234 |
Class Method Details
241 | 242 | 243 |245 | 246 | 247 | + (String) generate 248 | 249 | + (Array<String>) generate(quantity) 250 | 251 | + (Array<String>) generate(quantity, secure_random) 252 | 253 | 254 | 255 | 256 | 257 | 258 |
Returns one or more hashes based on the parameter supplied
261 | 262 | 263 |
435 | 436 | 437 | 438 | 79 439 | 80 440 | 81 441 | 82 442 | 83 443 | 84 444 | 85 445 | 86446 | |
447 |
448 | # File 'lib/cuid.rb', line 79 449 | 450 | def generate(quantity=1,secure_random=false) 451 | @use_secure_random = secure_random 452 | @fingerprint = get_fingerprint # only need to get the fingerprint once because it is constant per-run 453 | return api unless quantity > 1 454 | 455 | values = Array(1.upto(quantity)) # create an array of the correct size 456 | return values.collect { api } # fill array with hashes 457 | end458 | |
459 |
465 | 466 | + (Boolean) validate(str) 467 | 468 | 469 | 470 | 471 | 472 |
Validates (minimally) the supplied string is in the correct format to be a hash
475 | 476 |Validation checks that the first letter is correct and that the rest of the 477 | string is the correct length and consists of lower case letters and numbers.
478 | 479 | 480 |
524 | 525 | 526 | 527 | 96 528 | 97 529 | 98 530 | 99531 | |
532 |
533 | # File 'lib/cuid.rb', line 96 534 | 535 | def validate(str) 536 | blen = BLOCK_SIZE * 6 537 | !!str.match(/#{LETTER}[a-z0-9]{#{blen}}/) 538 | end539 | |
540 |