Sha256: 65843900571cd0c374c9f2c457b4d75bef6f428f34ee842e0ce505998bee4c88

Contents?: true

Size: 827 Bytes

Versions: 4

Compression:

Stored size: 827 Bytes

Contents

# frozen_string_literal: true

require 'ProtectedConstructor/version'

# @example
#   require 'protected_constructor'
#
#   class Klass
#     include ProtectedConstructor
#
#     def initialize
#     end
#   end
#
#   module KlassFactory
#     class << self
#       public
#       def create
#         Klass.send(:new)
#       end
#     end
#   end
#
#   # Constructor is protected.
#   klass = Klass.new # NoMethodError
#
#   # Example using factory...
#   klass = KlassFactory::create # works
#   klass.nil? # false
#   klass.is_a?(Klass) # true
#
#   # Example just using #send...
#   klass = Klass.send(:new) # works
#   klass.nil? # false
#   klass.is_a?(Klass) # true
#
module ProtectedConstructor
  def self.included(klass)
    klass.module_eval do
      class << self
        protected :new
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ProtectedConstructor-3.0.3 lib/protected_constructor.rb
ProtectedConstructor-3.0.2 lib/protected_constructor.rb
ProtectedConstructor-3.0.1 lib/protected_constructor.rb
ProtectedConstructor-3.0.0 lib/protected_constructor.rb