https://github.com/gangelo/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 and provided the examples; sorry, I don't remember the origin of the code to give credit. :S
https://github.com/gangelo/protectedconstructor
constructor gems rails ruby ruby-gem ruby-gems ruby-on-rails
Last synced: 3 days ago
JSON representation
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 and provided the examples; sorry, I don't remember the origin of the code to give credit. :S
- Host: GitHub
- URL: https://github.com/gangelo/protectedconstructor
- Owner: gangelo
- License: mit
- Created: 2013-07-08T13:57:07.000Z (over 12 years ago)
- Default Branch: main
- Last Pushed: 2024-11-04T07:00:45.000Z (over 1 year ago)
- Last Synced: 2025-05-07T13:05:00.817Z (9 months ago)
- Topics: constructor, gems, rails, ruby, ruby-gem, ruby-gems, ruby-on-rails
- Language: Ruby
- Homepage:
- Size: 54.7 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.txt
Awesome Lists containing this project
README
[](https://github.com/gangelo/ProtectedConstructor/actions/workflows/ruby.yml)
[](http://badge.fury.io/gh/gangelo%2FProtectedConstructor.svg)
[](https://badge.fury.io/rb/ProtectedConstructor.svg)
[](http://www.rubydoc.info/gems/ProtectedConstructor/)
[](https://github.com/gangelo/ProtectedConstructor/issues)
[](#license)
# 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 and provided the examples; 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 '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
## 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