Class: BuildTool::Singleton
- Inherits:
-
Object
- Object
- BuildTool::Singleton
- Defined in:
- lib/build-tool/singleton.rb
Direct Known Subclasses
Class Method Summary collapse
Instance Method Summary collapse
- #destroy ⇒ Object
-
#initialize ⇒ Singleton
constructor
A new instance of Singleton.
- #instance ⇒ Object
Constructor Details
#initialize ⇒ Singleton
Returns a new instance of Singleton.
22 23 24 25 26 27 |
# File 'lib/build-tool/singleton.rb', line 22 def initialize if !self.class.instance.nil? raise StandardError, "More than one instance of #{self.class} created!" end self.class.instance = self end |
Class Method Details
.inherited(child) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/build-tool/singleton.rb', line 7 def self.inherited( child ) if child.superclass == Singleton child.class_eval <<-EOS def self.instance; instance_variable_get( "@instance" ); end def self.instance=( x ); instance_variable_set( "@instance", x ); end EOS else child.class_eval <<-EOS def self.instance; superclass.instance; end def self.instance=( x ); superclass.instance = x; end EOS end child.instance_variable_set( "@instance", nil ) end |
.instance ⇒ Object
30 31 32 |
# File 'lib/build-tool/singleton.rb', line 30 def self.instance raise StandardError, "No instance of #{self} created" end |
Instance Method Details
#destroy ⇒ Object
34 35 36 37 38 39 40 |
# File 'lib/build-tool/singleton.rb', line 34 def destroy cls = self.class while cls.superclass != Singleton cls = cls.superclass end cls.instance = nil end |
#instance ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/build-tool/singleton.rb', line 42 def instance cls = self.class while cls.superclass != Singleton cls = cls.superclass end cls.instance end |