Class: ActiveUxid::Hash
Constant Summary
Constants inherited from Base
Class Method Summary collapse
Instance Method Summary collapse
- #decode_uxid ⇒ Object
- #encode_uxid ⇒ Object
-
#initialize(id) ⇒ Hash
constructor
A new instance of Hash.
- #uxid_decode_chars(id) ⇒ Object
- #uxid_encode_chars(id) ⇒ Object
Constructor Details
#initialize(id) ⇒ Hash
Returns a new instance of Hash.
8 9 10 |
# File 'lib/active_uxid/hash.rb', line 8 def initialize(id) @id = id end |
Class Method Details
.decode(id) ⇒ Object
17 18 19 20 |
# File 'lib/active_uxid/hash.rb', line 17 def self.decode(id) klass = new(id) klass.decode_uxid end |
.encode(id) ⇒ Object
12 13 14 15 |
# File 'lib/active_uxid/hash.rb', line 12 def self.encode(id) klass = new(id) klass.encode_uxid end |
Instance Method Details
#decode_uxid ⇒ Object
26 27 28 |
# File 'lib/active_uxid/hash.rb', line 26 def decode_uxid (uxid_decode_chars(@id) >> ENCODING_LENGTH) - ENCODING_SALT end |
#encode_uxid ⇒ Object
22 23 24 |
# File 'lib/active_uxid/hash.rb', line 22 def encode_uxid uxid_encode_chars((@id + ENCODING_SALT) << ENCODING_LENGTH) end |
#uxid_decode_chars(id) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/active_uxid/hash.rb', line 44 def uxid_decode_chars(id) pos = 0 num = 0 len = id.length max = len - 1 while pos < len pow = ENCODING_BASE**(max - pos) num += ENCODING_CHARS.index(id[pos]) * pow pos += 1 end num end |
#uxid_encode_chars(id) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/active_uxid/hash.rb', line 30 def uxid_encode_chars(id) return '0' if id.zero? return nil if id.negative? str = '' while id.positive? str = "#{ENCODING_CHARS[id % ENCODING_BASE]}#{str}" id /= ENCODING_BASE end str end |