[ruby/singleton] Revert "Simplify the implementation
(https://github.com/ruby/singleton/pull/7)" This reverts commit https://github.com/ruby/singleton/commit/545b6b61a40d. This change break Rails CI: https://bugs.ruby-lang.org/issues/19711 https://github.com/ruby/singleton/commit/911531d508
This commit is contained in:
parent
068672cfe8
commit
3a4302c742
@ -112,7 +112,7 @@ module Singleton
|
|||||||
module SingletonClassMethods # :nodoc:
|
module SingletonClassMethods # :nodoc:
|
||||||
|
|
||||||
def clone # :nodoc:
|
def clone # :nodoc:
|
||||||
super.include(Singleton)
|
Singleton.__init__(super)
|
||||||
end
|
end
|
||||||
|
|
||||||
# By default calls instance(). Override to retain singleton state.
|
# By default calls instance(). Override to retain singleton state.
|
||||||
@ -121,18 +121,31 @@ module Singleton
|
|||||||
end
|
end
|
||||||
|
|
||||||
def instance # :nodoc:
|
def instance # :nodoc:
|
||||||
@singleton__instance__ || @singleton__mutex__.synchronize { @singleton__instance__ ||= new }
|
return @singleton__instance__ if @singleton__instance__
|
||||||
|
@singleton__mutex__.synchronize {
|
||||||
|
return @singleton__instance__ if @singleton__instance__
|
||||||
|
@singleton__instance__ = new()
|
||||||
|
}
|
||||||
|
@singleton__instance__
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def inherited(sub_klass)
|
def inherited(sub_klass)
|
||||||
super
|
super
|
||||||
sub_klass.include(Singleton)
|
Singleton.__init__(sub_klass)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class << Singleton # :nodoc:
|
class << Singleton # :nodoc:
|
||||||
|
def __init__(klass) # :nodoc:
|
||||||
|
klass.instance_eval {
|
||||||
|
@singleton__instance__ = nil
|
||||||
|
@singleton__mutex__ = Thread::Mutex.new
|
||||||
|
}
|
||||||
|
klass
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
# extending an object with Singleton is a bad idea
|
# extending an object with Singleton is a bad idea
|
||||||
@ -143,19 +156,14 @@ module Singleton
|
|||||||
unless mod.instance_of?(Class)
|
unless mod.instance_of?(Class)
|
||||||
raise TypeError, "Inclusion of the OO-Singleton module in module #{mod}"
|
raise TypeError, "Inclusion of the OO-Singleton module in module #{mod}"
|
||||||
end
|
end
|
||||||
|
|
||||||
super
|
super
|
||||||
end
|
end
|
||||||
|
|
||||||
def included(klass)
|
def included(klass)
|
||||||
super
|
super
|
||||||
|
|
||||||
klass.private_class_method :new, :allocate
|
klass.private_class_method :new, :allocate
|
||||||
klass.extend SingletonClassMethods
|
klass.extend SingletonClassMethods
|
||||||
klass.instance_eval {
|
Singleton.__init__(klass)
|
||||||
@singleton__instance__ = nil
|
|
||||||
@singleton__mutex__ = Thread::Mutex.new
|
|
||||||
}
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -94,13 +94,6 @@ class TestSingleton < Test::Unit::TestCase
|
|||||||
assert_same a, b
|
assert_same a, b
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_inheritance_creates_separate_singleton
|
|
||||||
a = SingletonTest.instance
|
|
||||||
b = Class.new(SingletonTest).instance
|
|
||||||
|
|
||||||
assert_not_same a, b
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_class_level_cloning_preserves_singleton_behavior
|
def test_class_level_cloning_preserves_singleton_behavior
|
||||||
klass = SingletonTest.clone
|
klass = SingletonTest.clone
|
||||||
|
|
||||||
@ -108,8 +101,4 @@ class TestSingleton < Test::Unit::TestCase
|
|||||||
b = klass.instance
|
b = klass.instance
|
||||||
assert_same a, b
|
assert_same a, b
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_class_level_cloning_creates_separate_singleton
|
|
||||||
assert_not_same SingletonTest.instance, SingletonTest.clone.instance
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user