Module: ProtectedConstructor

Defined in:
lib/protected_constructor.rb,
lib/ProtectedConstructor/version.rb

Overview

Examples:

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

Constant Summary collapse

VERSION =
'4.0.0'

Class Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/protected_constructor.rb', line 38

def self.included(klass)
  klass.module_eval do
    class << self
      protected :new
    end
  end
end