Module: Singleton

Included in:
A, Middle, SomeSingletonClass
Defined in:
lib/singleton.rb

Defined Under Namespace

Modules: SingletonClassMethods

Constant Summary collapse

FirstInstanceCall =

Method body of first instance call.

proc do
  #  @__instance__ takes on one of the following values
  #  * nil     -  before and after a failed creation
  #  * false  -  during creation
  #  * sub_class instance  -  after a successful creation
  #  the form makes up for the lack of returns in progs
  Thread.critical = true
  if  @__instance__.nil?
    @__instance__  = false
    Thread.critical = false
    begin
      @__instance__ = new
    ensure
      if @__instance__
        class <<self
          remove_method :instance
          def instance; @__instance__ end
        end
      else
        @__instance__ = nil #  failed instance creation
      end
    end
  elsif  _instantiate?()
    Thread.critical = false
  else
    @__instance__  = false
    Thread.critical = false
    begin
      @__instance__ = new
    ensure
      if @__instance__
        class <<self
          remove_method :instance
          def instance; @__instance__ end
        end
      else
        @__instance__ = nil
      end
    end
  end
  @__instance__
end

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.__init__(klass) ⇒ Object



156
157
158
159
160
161
162
# File 'lib/singleton.rb', line 156

def __init__(klass)
  klass.instance_eval { @__instance__ = nil }
  class << klass
    define_method(:instance,FirstInstanceCall)
  end
  klass
end

Instance Method Details

#_dump(depth = 1) ⇒ Object

default marshalling strategy



75
76
77
# File 'lib/singleton.rb', line 75

def _dump(depth=-1)
  ''
end

#cloneObject

disable build-in copying methods

Raises:

  • (TypeError)


67
68
69
# File 'lib/singleton.rb', line 67

def clone
  raise TypeError, "can't clone instance of singleton #{self.class}"
end

#dupObject

Raises:

  • (TypeError)


70
71
72
# File 'lib/singleton.rb', line 70

def dup
  raise TypeError, "can't dup instance of singleton #{self.class}"
end