Sha256: fcc0f29950e28c07afe0f485c005433e13877210e78aaf49819843d16af21d43

Contents?: true

Size: 605 Bytes

Versions: 2

Compression:

Stored size: 605 Bytes

Contents

# frozen_string_literal: true

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

module Peddler
  module Errors
    # @api 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
          return Errors.const_get(name) if Errors.const_defined?(name)

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

      private

      def with_mutex
        @mutex.synchronize { yield }
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
peddler-2.1.1 lib/peddler/errors/class_generator.rb
peddler-2.1.0 lib/peddler/errors/class_generator.rb