Sha256: 7040da0afe20191bd2818999655f4854092e712bf261103016c46ad4bb3dc4e5

Contents?: true

Size: 928 Bytes

Versions: 5

Compression:

Stored size: 928 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

5 entries across 5 versions & 1 rubygems

Version Path
lite-uxid-1.0.7 lib/lite/uxid/ulid.rb
lite-uxid-1.0.6 lib/lite/uxid/ulid.rb
lite-uxid-1.0.5 lib/lite/uxid/ulid.rb
lite-uxid-1.0.4 lib/lite/uxid/ulid.rb
lite-uxid-1.0.3 lib/lite/uxid/ulid.rb