Sha256: 02707e339002e5931de551271c92600d6c499703e7703c8e6b5e1eddfde11a35

Contents?: true

Size: 762 Bytes

Versions: 3

Compression:

Stored size: 762 Bytes

Contents

# frozen_string_literal: true

require 'singleton'
require 'peddler/errors/error'

module Peddler
  module Errors
    # @!visibility private
    class ClassGenerator
      include Singleton

      def self.call(name)
        instance.generate(name)
      end

      def initialize
        @mutex = Mutex.new
      end

      def generate(name)
        with_mutex do
          if Errors.const_defined?(name)
            error = Errors.const_get(name)
            return error if error <= Error

            raise TypeError, "#{name} must be a Peddler::Errors::Error"
          end

          Errors.const_set(name, Class.new(Error))
        end
      end

      private

      def with_mutex(&block)
        @mutex.synchronize(&block)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
peddler-2.4.5 lib/peddler/errors/class_generator.rb
peddler-2.4.4 lib/peddler/errors/class_generator.rb
peddler-2.4.3 lib/peddler/errors/class_generator.rb