Sha256: 0805a9ab53ecf20212f499f2fc3f1751463a8ff19c73cfba58dfb32c78f6366e
Contents?: true
Size: 1.32 KB
Versions: 3
Compression:
Stored size: 1.32 KB
Contents
# ProtectedConstructor Provides a module that may be included in a Ruby class, that protects the constructor; good for enforcing instantiation of classes using, for instance, a class factory. The code itself is not mine, I just wrapped it in a gem; sorry, I don't remember the origin of the code to give credit. ## Installation Add this line to your application's Gemfile: gem 'ProtectedConstructor' And then execute: $ bundle Or install it yourself as: $ gem install ProtectedConstructor ## Usage require 'ProtectedConstructor' class Klass include ProtectedConstructor def initialize end end module KlassFactory class << self public def create Klass.send(:new) end end end Constructor is not 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 ## Contributing 1. Fork it 2. Create your feature branch (`git checkout -b my-new-feature`) 3. Commit your changes (`git commit -am 'Add some feature'`) 4. Push to the branch (`git push origin my-new-feature`) 5. Create new Pull Request
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
ProtectedConstructor-1.0.3 | README.md |
ProtectedConstructor-1.0.2 | README.md |
ProtectedConstructor-1.0.1 | README.md |