Sha256: c4f320d9b1cd5bf431f0903a1c73a4ab4964d7fb4a48617beb0d1c2bb810decf

Contents?: true

Size: 1001 Bytes

Versions: 4

Compression:

Stored size: 1001 Bytes

Contents

# frozen_string_literal: true

module ActiveUxid
  class Hash < ActiveUxid::Base

    def initialize(id)
      @id = id
    end

    def self.encode(id)
      klass = new(id)
      klass.encode_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 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?

      str = ''

      while id.positive?
        str = "#{ENCODING_CHARS[id % ENCODING_BASE]}#{str}"
        id /= ENCODING_BASE
      end

      str
    end

    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

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
active_uxid-1.0.3 lib/active_uxid/hash.rb
active_uxid-1.0.2 lib/active_uxid/hash.rb
active_uxid-1.0.1 lib/active_uxid/hash.rb
active_uxid-1.0.0 lib/active_uxid/hash.rb