Class: BuildTool::Singleton

Inherits:
Object
  • Object
show all
Defined in:
lib/build-tool/singleton.rb

Direct Known Subclasses

Application

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSingleton

Returns a new instance of Singleton.



20
21
22
23
24
25
# File 'lib/build-tool/singleton.rb', line 20

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



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/build-tool/singleton.rb', line 5

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

.instanceObject

Raises:

  • (StandardError)


28
29
30
# File 'lib/build-tool/singleton.rb', line 28

def self.instance
    raise StandardError, "No instance of #{self} created"
end

Instance Method Details

#destroyObject



32
33
34
35
36
37
38
# File 'lib/build-tool/singleton.rb', line 32

def destroy
    cls = self.class
    while cls.superclass != Singleton
        cls = cls.superclass
    end
    cls.instance = nil
end

#instanceObject



40
41
42
43
44
45
46
# File 'lib/build-tool/singleton.rb', line 40

def instance
    cls = self.class
    while cls.superclass != Singleton
        cls = cls.superclass
    end
    cls.instance
end