Sha256: bfe13cb4539b40a636a990ba300fbf380139841af1ce2ff99461bf8fcbc652fe

Contents?: true

Size: 926 Bytes

Versions: 2

Compression:

Stored size: 926 Bytes

Contents

# frozen_string_literal: true

require 'securerandom'

module Lite
  module Uxid
    class Ulid < Lite::Uxid::Base

      MASK = 0x1f

      class << self

        def encode
          klass = new
          klass.encode
        end

      end

      def encode
        oct = octect
        ele = '0' * encoding_length
        pos = encoding_length - 1

        while oct.positive?
          ele[pos] = encoding_chars[oct & MASK]
          oct >>= 5
          pos -= 1
        end

        ele
      end

      private

      def bytes
        "#{unixtime_48bit}#{SecureRandom.random_bytes(10)}"
      end

      def octect
        (hi, lo) = bytes.unpack('Q>Q>')
        (hi << 64) | lo
      end

      def unixtime_ms
        time = Time.respond_to?(:current) ? Time.current : Time.now
        (time.to_f * 1_000).to_i
      end

      def unixtime_48bit
        [unixtime_ms].pack('Q>')[2..-1]
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
lite-uxid-1.1.0 lib/lite/uxid/ulid.rb
lite-uxid-1.0.8 lib/lite/uxid/ulid.rb