2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2011-05-17 14:03:19 -04:00
|
|
|
|
2002-01-29 02:16:09 -05:00
|
|
|
# The Singleton module implements the Singleton pattern.
|
2001-11-27 05:00:35 -05:00
|
|
|
#
|
2011-05-17 14:03:19 -04:00
|
|
|
# == Usage
|
|
|
|
#
|
|
|
|
# To use Singleton, include the module in your class.
|
|
|
|
#
|
2002-01-29 02:16:09 -05:00
|
|
|
# class Klass
|
|
|
|
# include Singleton
|
|
|
|
# # ...
|
|
|
|
# end
|
2001-11-27 05:00:35 -05:00
|
|
|
#
|
2011-05-17 14:03:19 -04:00
|
|
|
# This ensures that only one instance of Klass can be created.
|
2001-11-27 05:00:35 -05:00
|
|
|
#
|
2020-03-14 18:20:09 -04:00
|
|
|
# a,b = Klass.instance, Klass.instance
|
2011-05-18 10:09:38 -04:00
|
|
|
#
|
|
|
|
# a == b
|
2011-05-17 14:03:19 -04:00
|
|
|
# # => true
|
|
|
|
#
|
2011-05-18 10:09:38 -04:00
|
|
|
# Klass.new
|
2011-05-17 14:03:19 -04:00
|
|
|
# # => NoMethodError - new is private ...
|
2001-11-27 05:00:35 -05:00
|
|
|
#
|
2011-05-17 14:03:19 -04:00
|
|
|
# The instance is created at upon the first call of Klass.instance().
|
2001-11-27 05:00:35 -05:00
|
|
|
#
|
2005-03-15 09:52:59 -05:00
|
|
|
# class OtherKlass
|
2001-11-27 05:00:35 -05:00
|
|
|
# include Singleton
|
|
|
|
# # ...
|
2005-03-15 09:52:59 -05:00
|
|
|
# end
|
2001-11-27 05:00:35 -05:00
|
|
|
#
|
2011-05-18 10:09:38 -04:00
|
|
|
# ObjectSpace.each_object(OtherKlass){}
|
2011-05-17 14:03:19 -04:00
|
|
|
# # => 0
|
2001-11-27 05:00:35 -05:00
|
|
|
#
|
2011-05-17 14:03:19 -04:00
|
|
|
# OtherKlass.instance
|
2011-05-18 10:09:38 -04:00
|
|
|
# ObjectSpace.each_object(OtherKlass){}
|
2011-05-17 14:03:19 -04:00
|
|
|
# # => 1
|
2001-11-27 05:00:35 -05:00
|
|
|
#
|
2003-08-02 14:05:02 -04:00
|
|
|
#
|
2011-05-17 14:03:19 -04:00
|
|
|
# This behavior is preserved under inheritance and cloning.
|
2003-08-02 14:05:02 -04:00
|
|
|
#
|
2011-05-17 14:03:19 -04:00
|
|
|
# == Implementation
|
2001-11-27 05:00:35 -05:00
|
|
|
#
|
2011-05-17 14:03:19 -04:00
|
|
|
# This above is achieved by:
|
2003-08-02 14:05:02 -04:00
|
|
|
#
|
2011-05-17 14:03:19 -04:00
|
|
|
# * Making Klass.new and Klass.allocate private.
|
2003-08-02 14:05:02 -04:00
|
|
|
#
|
2011-05-17 14:03:19 -04:00
|
|
|
# * Overriding Klass.inherited(sub_klass) and Klass.clone() to ensure that the
|
|
|
|
# Singleton properties are kept when inherited and cloned.
|
2003-08-02 14:05:02 -04:00
|
|
|
#
|
2011-05-17 14:03:19 -04:00
|
|
|
# * Providing the Klass.instance() method that returns the same object each
|
|
|
|
# time it is called.
|
2003-08-02 14:05:02 -04:00
|
|
|
#
|
2011-05-17 14:03:19 -04:00
|
|
|
# * Overriding Klass._load(str) to call Klass.instance().
|
2003-08-02 14:05:02 -04:00
|
|
|
#
|
2011-05-18 10:09:38 -04:00
|
|
|
# * Overriding Klass#clone and Klass#dup to raise TypeErrors to prevent
|
2011-05-17 14:03:19 -04:00
|
|
|
# cloning or duping.
|
|
|
|
#
|
|
|
|
# == Singleton and Marshal
|
|
|
|
#
|
|
|
|
# By default Singleton's #_dump(depth) returns the empty string. Marshalling by
|
2019-09-24 23:59:12 -04:00
|
|
|
# default will strip state information, e.g. instance variables from the instance.
|
|
|
|
# Classes using Singleton can provide custom _load(str) and _dump(depth) methods
|
|
|
|
# to retain some of the previous state of the instance.
|
2011-05-17 14:03:19 -04:00
|
|
|
#
|
|
|
|
# require 'singleton'
|
2011-05-18 10:09:38 -04:00
|
|
|
#
|
2011-05-17 14:03:19 -04:00
|
|
|
# class Example
|
|
|
|
# include Singleton
|
|
|
|
# attr_accessor :keep, :strip
|
|
|
|
# def _dump(depth)
|
|
|
|
# # this strips the @strip information from the instance
|
|
|
|
# Marshal.dump(@keep, depth)
|
|
|
|
# end
|
2011-05-18 10:09:38 -04:00
|
|
|
#
|
2011-05-17 14:03:19 -04:00
|
|
|
# def self._load(str)
|
|
|
|
# instance.keep = Marshal.load(str)
|
|
|
|
# instance
|
|
|
|
# end
|
|
|
|
# end
|
2011-05-18 10:09:38 -04:00
|
|
|
#
|
2011-05-17 14:03:19 -04:00
|
|
|
# a = Example.instance
|
|
|
|
# a.keep = "keep this"
|
|
|
|
# a.strip = "get rid of this"
|
2011-05-18 10:09:38 -04:00
|
|
|
#
|
2011-05-17 14:03:19 -04:00
|
|
|
# stored_state = Marshal.dump(a)
|
2011-05-18 10:09:38 -04:00
|
|
|
#
|
2011-05-17 14:03:19 -04:00
|
|
|
# a.keep = nil
|
|
|
|
# a.strip = nil
|
|
|
|
# b = Marshal.load(stored_state)
|
|
|
|
# p a == b # => true
|
|
|
|
# p a.keep # => "keep this"
|
|
|
|
# p a.strip # => nil
|
2001-11-27 05:00:35 -05:00
|
|
|
#
|
1999-08-13 01:45:20 -04:00
|
|
|
module Singleton
|
2011-05-17 14:03:19 -04:00
|
|
|
# Raises a TypeError to prevent cloning.
|
2003-08-02 14:05:02 -04:00
|
|
|
def clone
|
|
|
|
raise TypeError, "can't clone instance of singleton #{self.class}"
|
|
|
|
end
|
2011-05-17 14:03:19 -04:00
|
|
|
|
|
|
|
# Raises a TypeError to prevent duping.
|
2003-08-02 14:05:02 -04:00
|
|
|
def dup
|
|
|
|
raise TypeError, "can't dup instance of singleton #{self.class}"
|
|
|
|
end
|
2008-05-28 02:18:18 -04:00
|
|
|
|
2011-05-17 14:03:19 -04:00
|
|
|
# By default, do not retain any state when marshalling.
|
2008-05-28 02:18:18 -04:00
|
|
|
def _dump(depth = -1)
|
2003-08-02 14:05:02 -04:00
|
|
|
''
|
|
|
|
end
|
|
|
|
|
2011-05-17 14:03:19 -04:00
|
|
|
module SingletonClassMethods # :nodoc:
|
|
|
|
|
|
|
|
def clone # :nodoc:
|
2003-08-02 14:05:02 -04:00
|
|
|
Singleton.__init__(super)
|
|
|
|
end
|
2006-12-31 10:02:22 -05:00
|
|
|
|
2011-05-17 14:03:19 -04:00
|
|
|
# By default calls instance(). Override to retain singleton state.
|
2008-05-28 02:18:18 -04:00
|
|
|
def _load(str)
|
|
|
|
instance
|
|
|
|
end
|
|
|
|
|
2019-06-28 01:23:22 -04:00
|
|
|
def instance # :nodoc:
|
|
|
|
return @singleton__instance__ if @singleton__instance__
|
|
|
|
@singleton__mutex__.synchronize {
|
|
|
|
return @singleton__instance__ if @singleton__instance__
|
|
|
|
@singleton__instance__ = new()
|
|
|
|
}
|
|
|
|
@singleton__instance__
|
|
|
|
end
|
|
|
|
|
2003-08-02 14:05:02 -04:00
|
|
|
private
|
2008-05-28 02:18:18 -04:00
|
|
|
|
2003-08-02 14:05:02 -04:00
|
|
|
def inherited(sub_klass)
|
|
|
|
super
|
|
|
|
Singleton.__init__(sub_klass)
|
|
|
|
end
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
2011-05-17 14:03:19 -04:00
|
|
|
class << Singleton # :nodoc:
|
|
|
|
def __init__(klass) # :nodoc:
|
2006-12-31 10:02:22 -05:00
|
|
|
klass.instance_eval {
|
2007-02-14 11:50:16 -05:00
|
|
|
@singleton__instance__ = nil
|
2016-08-30 02:22:30 -04:00
|
|
|
@singleton__mutex__ = Thread::Mutex.new
|
2006-12-31 10:02:22 -05:00
|
|
|
}
|
|
|
|
klass
|
2001-11-27 05:00:35 -05:00
|
|
|
end
|
2008-05-28 02:18:18 -04:00
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
private
|
|
|
|
|
2011-05-17 14:03:19 -04:00
|
|
|
# extending an object with Singleton is a bad idea
|
2006-12-31 10:02:22 -05:00
|
|
|
undef_method :extend_object
|
2008-05-28 02:18:18 -04:00
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
def append_features(mod)
|
|
|
|
# help out people counting on transitive mixins
|
|
|
|
unless mod.instance_of?(Class)
|
|
|
|
raise TypeError, "Inclusion of the OO-Singleton module in module #{mod}"
|
|
|
|
end
|
|
|
|
super
|
2003-08-02 14:05:02 -04:00
|
|
|
end
|
2008-05-28 02:18:18 -04:00
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
def included(klass)
|
|
|
|
super
|
2011-05-17 14:03:19 -04:00
|
|
|
klass.private_class_method :new, :allocate
|
2006-12-31 10:02:22 -05:00
|
|
|
klass.extend SingletonClassMethods
|
|
|
|
Singleton.__init__(klass)
|
2003-08-02 14:05:02 -04:00
|
|
|
end
|
|
|
|
end
|
2008-05-28 02:18:18 -04:00
|
|
|
|
2011-05-17 14:03:19 -04:00
|
|
|
##
|
|
|
|
# :singleton-method: _load
|
|
|
|
# By default calls instance(). Override to retain singleton state.
|
2019-06-28 01:37:52 -04:00
|
|
|
|
|
|
|
##
|
|
|
|
# :singleton-method: instance
|
|
|
|
# Returns the singleton instance.
|
1999-08-13 01:45:20 -04:00
|
|
|
end
|