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.



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

.instanceObject

Raises:

  • (StandardError)


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

#destroyObject



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

#instanceObject



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