app/models/coupdoeil/hovercard/registry.rb



# frozen_string_literal: true

module Coupdoeil
  class Hovercard
    class Registry
      attr_reader :registry, :semaphore

      def initialize
        @semaphore = Mutex.new
        @registry = Hash.new
      end

      def register(type, klass) = @semaphore.synchronize { @registry[type] = klass }
      def lookup(type) = @semaphore.synchronize {@registry.fetch(type) }
      def safe_lookup(type) = @semaphore.synchronize {@registry[type] }

      def lookup_or_register(type)
        safe_lookup(type) ||
          begin
            register(type, (type.classify + "Hovercard").constantize)
          end
      end
    end
  end
end