Sha256: f59beba22666721f608335f92787f906b7c875bfa2dc4edfabb3673de5e0d473

Contents?: true

Size: 613 Bytes

Versions: 2

Compression:

Stored size: 613 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
          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.4.0 lib/peddler/errors/class_generator.rb
peddler-2.3.0 lib/peddler/errors/class_generator.rb