Class: UID
- Inherits:
-
Object
- Object
- UID
- Defined in:
- lib/userify/uid.rb
Overview
Usage:
UID.new.to_s => “aWgEPTl1tmebfsQzFP4bxwgy80V” UID.new(5).to_s => “8FsD5” UID.new.to_i => 838068072552051698674007079806269848286804777409 1000000000.base62 => “15ftgG” “123abcABC”.base62 => 225587272106046
By default, UIDs generates random BASE62 string of 27 characters, which is safer than 160bit SHA-1.
>> (‘f’*40).hex
> 1461501637330902918203684832716283019655932542975
>> (‘z’*27).base62
> 2480707824361381849652170924082266893544595652607
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(n = 27) ⇒ UID
constructor
A new instance of UID.
- #to_hex ⇒ Object
- #to_i ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(n = 27) ⇒ UID
Returns a new instance of UID.
19 20 21 22 |
# File 'lib/userify/uid.rb', line 19 def initialize(n=27) max = ("Z"*n).base62 @value = SecureRandom.random_number(max) end |
Class Method Details
.generate(n = 6) ⇒ Object
36 37 38 |
# File 'lib/userify/uid.rb', line 36 def self.generate(n=6) [*0..9,*?A..?Z,*?a..?z].sample(n).join end |
Instance Method Details
#to_hex ⇒ Object
32 33 34 |
# File 'lib/userify/uid.rb', line 32 def to_hex @value.to_s(16) end |
#to_i ⇒ Object
28 29 30 |
# File 'lib/userify/uid.rb', line 28 def to_i @value end |
#to_s ⇒ Object
24 25 26 |
# File 'lib/userify/uid.rb', line 24 def to_s @value.base62 end |