Sha256: 3c88e619545cbc2262adeef0f1d79c47116a9ad40d3271c198cf624a043eb72a

Contents?: true

Size: 962 Bytes

Versions: 3

Compression:

Stored size: 962 Bytes

Contents

require "digest/sha1"
require "socket"

class PropertybaseId
  class Generator
    def initialize
      @counter = 0
      @mutex = Mutex.new
    end

    def generate(object: )
      @mutex.synchronize do
        PropertybaseId.new(
          object_id: pb_object_id(object),
          host_id: host_id,
          time: ::Time.now.to_i,
          process_id: process_id,
          counter: next_counter
        )
      end
    end

    private

    def pb_object_id(object)
      PropertybaseId::Mappings.objects.fetch(object.to_s) do
        raise ArgumentError, "Object #{object.inspect} not found"
      end
    end

    def host_id
      @_host_id ||= Digest::SHA1.hexdigest(Socket.gethostname).to_i(16) % PropertybaseId.max_value(2)
    end

    def next_counter
      @counter = (@counter + 1) % PropertybaseId.max_value(5)
    end

    def process_id
      "#{Process.pid}#{Thread.current.object_id}".hash % PropertybaseId.max_value(2)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
propertybase_id-0.5.0 lib/propertybase_id/generator.rb
propertybase_id-0.4.0 lib/propertybase_id/generator.rb
propertybase_id-0.3.3 lib/propertybase_id/generator.rb