lib/active_uxid/hash.rb in active_uxid-5.0.1 vs lib/active_uxid/hash.rb in active_uxid-5.0.2

- old
+ new

@@ -1,59 +1,57 @@ # frozen_string_literal: true -module ActiveUxid - class Hash < ActiveUxid::Base +class ActiveUxid::Hash < ActiveUxid::Base - def initialize(id) - @id = id - super() - end + def initialize(id) + @id = id + super() + end - def self.encode(id) - klass = new(id) - klass.encode_uxid - end + def self.encode(id) + klass = new(id) + klass.encode_uxid + end - def self.decode(id) - klass = new(id) - klass.decode_uxid - end + def self.decode(id) + klass = new(id) + klass.decode_uxid + end - def encode_uxid - uxid_encode_chars((@id + encoding_salt) << encoding_length) - end + def encode_uxid + uxid_encode_chars((@id + encoding_salt) << encoding_length) + end - def decode_uxid - (uxid_decode_chars(@id) >> encoding_length) - encoding_salt - end + def decode_uxid + (uxid_decode_chars(@id) >> encoding_length) - encoding_salt + end - def uxid_encode_chars(id) - return '0' if id.zero? - return nil if id.negative? + def uxid_encode_chars(id) + return '0' if id.zero? + return nil if id.negative? - str = '' + str = '' - while id.positive? - str = "#{encoding_chars[id % encoding_base]}#{str}" - id /= encoding_base - end - - str + while id.positive? + str = "#{encoding_chars[id % encoding_base]}#{str}" + id /= encoding_base end - def uxid_decode_chars(id) - pos = 0 - num = 0 - len = id.length - max = len - 1 + str + end - while pos < len - pow = encoding_base**(max - pos) - num += encoding_chars.index(id[pos]) * pow - pos += 1 - end + def uxid_decode_chars(id) + pos = 0 + num = 0 + len = id.length + max = len - 1 - num + while pos < len + pow = encoding_base**(max - pos) + num += encoding_chars.index(id[pos]) * pow + pos += 1 end + num end + end