lib/active_uxid/hash.rb in active_uxid-1.0.11 vs lib/active_uxid/hash.rb in active_uxid-1.0.12
- old
+ new
@@ -1,14 +1,13 @@
# frozen_string_literal: true
module ActiveUxid
class Hash < ActiveUxid::Base
- ENCODING_BASE ||= ENCODING_CHARS.length
-
def initialize(id)
@id = id
+ super()
end
def self.encode(id)
klass = new(id)
klass.encode_uxid
@@ -18,26 +17,26 @@
klass = new(id)
klass.decode_uxid
end
def encode_uxid
- uxid_encode_chars((@id + ENCODING_SALT) << ENCODING_LENGTH)
+ uxid_encode_chars((@id + encoding_salt) << encoding_length)
end
def decode_uxid
- (uxid_decode_chars(@id) >> ENCODING_LENGTH) - ENCODING_SALT
+ (uxid_decode_chars(@id) >> encoding_length) - encoding_salt
end
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
+ str = "#{encoding_chars[id % encoding_base]}#{str}"
+ id /= encoding_base
end
str
end
@@ -46,11 +45,11 @@
num = 0
len = id.length
max = len - 1
while pos < len
- pow = ENCODING_BASE**(max - pos)
- num += ENCODING_CHARS.index(id[pos]) * pow
+ pow = encoding_base ** (max - pos)
+ num += encoding_chars.index(id[pos]) * pow
pos += 1
end
num
end