2015-12-16 08:07:31 +03:00
|
|
|
# frozen_string_literal: false
|
2011-05-17 22:03:19 +04:00
|
|
|
|
2002-01-29 10:16:09 +03:00
|
|
|
# The Singleton module implements the Singleton pattern.
|
2001-11-27 13:00:35 +03:00
|
|
|
#
|
2011-05-17 22:03:19 +04:00
|
|
|
# == Usage
|
|
|
|
#
|
|
|
|
# To use Singleton, include the module in your class.
|
|
|
|
#
|
2002-01-29 10:16:09 +03:00
|
|
|
# class Klass
|
|
|
|
# include Singleton
|
|
|
|
# # ...
|
|
|
|
# end
|
2001-11-27 13:00:35 +03:00
|
|
|
#
|
2011-05-17 22:03:19 +04:00
|
|
|
# This ensures that only one instance of Klass can be created.
|
2001-11-27 13:00:35 +03:00
|
|
|
#
|
2020-03-15 01:20:09 +03:00
|
|
|
# a,b = Klass.instance, Klass.instance
|
2011-05-18 18:09:38 +04:00
|
|
|
#
|
|
|
|
# a == b
|
2011-05-17 22:03:19 +04:00
|
|
|
# # => true
|
|
|
|
#
|
2011-05-18 18:09:38 +04:00
|
|
|
# Klass.new
|
2011-05-17 22:03:19 +04:00
|
|
|
# # => NoMethodError - new is private ...
|
2001-11-27 13:00:35 +03:00
|
|
|
#
|
2011-05-17 22:03:19 +04:00
|
|
|
# The instance is created at upon the first call of Klass.instance().
|
2001-11-27 13:00:35 +03:00
|
|
|
#
|
2005-03-15 17:52:59 +03:00
|
|
|
# class OtherKlass
|
2001-11-27 13:00:35 +03:00
|
|
|
# include Singleton
|
|
|
|
# # ...
|
2005-03-15 17:52:59 +03:00
|
|
|
# end
|
2001-11-27 13:00:35 +03:00
|
|
|
#
|
2011-05-18 18:09:38 +04:00
|
|
|
# ObjectSpace.each_object(OtherKlass){}
|
2011-05-17 22:03:19 +04:00
|
|
|
# # => 0
|
2001-11-27 13:00:35 +03:00
|
|
|
#
|
2011-05-17 22:03:19 +04:00
|
|
|
# OtherKlass.instance
|
2011-05-18 18:09:38 +04:00
|
|
|
# ObjectSpace.each_object(OtherKlass){}
|
2011-05-17 22:03:19 +04:00
|
|
|
# # => 1
|
2001-11-27 13:00:35 +03:00
|
|
|
#
|
2003-08-02 22:05:02 +04:00
|
|
|
#
|
2011-05-17 22:03:19 +04:00
|
|
|
# This behavior is preserved under inheritance and cloning.
|
2003-08-02 22:05:02 +04:00
|
|
|
#
|
2011-05-17 22:03:19 +04:00
|
|
|
# == Implementation
|
2001-11-27 13:00:35 +03:00
|
|
|
#
|
2011-05-17 22:03:19 +04:00
|
|
|
# This above is achieved by:
|
2003-08-02 22:05:02 +04:00
|
|
|
#
|
2011-05-17 22:03:19 +04:00
|
|
|
# * Making Klass.new and Klass.allocate private.
|
2003-08-02 22:05:02 +04:00
|
|
|
#
|
2011-05-17 22: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 22:05:02 +04:00
|
|
|
#
|
2011-05-17 22:03:19 +04:00
|
|
|
# * Providing the Klass.instance() method that returns the same object each
|
|
|
|
# time it is called.
|
2003-08-02 22:05:02 +04:00
|
|
|
#
|
2011-05-17 22:03:19 +04:00
|
|
|
# * Overriding Klass._load(str) to call Klass.instance().
|
2003-08-02 22:05:02 +04:00
|
|
|
#
|
2011-05-18 18:09:38 +04:00
|
|
|
# * Overriding Klass#clone and Klass#dup to raise TypeErrors to prevent
|
2011-05-17 22:03:19 +04:00
|
|
|
# cloning or duping.
|
|
|
|
#
|
|
|
|
# == Singleton and Marshal
|
|
|
|
#
|
|
|
|
# By default Singleton's #_dump(depth) returns the empty string. Marshalling by
|
2019-09-25 06:59:12 +03: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 22:03:19 +04:00
|
|
|
#
|
|
|
|
# require 'singleton'
|
2011-05-18 18:09:38 +04:00
|
|
|
#
|
2011-05-17 22: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 18:09:38 +04:00
|
|
|
#
|
2011-05-17 22:03:19 +04:00
|
|
|
# def self._load(str)
|
|
|
|
# instance.keep = Marshal.load(str)
|
|
|
|
# instance
|
|
|
|
# end
|
|
|
|
# end
|
2011-05-18 18:09:38 +04:00
|
|
|
#
|
2011-05-17 22:03:19 +04:00
|
|
|
# a = Example.instance
|
|
|
|
# a.keep = "keep this"
|
|
|
|
# a.strip = "get rid of this"
|
2011-05-18 18:09:38 +04:00
|
|
|
#
|
2011-05-17 22:03:19 +04:00
|
|
|
# stored_state = Marshal.dump(a)
|
2011-05-18 18:09:38 +04:00
|
|
|
#
|
2011-05-17 22: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 13:00:35 +03:00
|
|
|
#
|
1999-08-13 09:45:20 +04:00
|
|
|
module Singleton
|
2023-11-06 12:43:52 +03:00
|
|
|
VERSION = "0.2.0"
|
2020-07-28 18:31:52 +03:00
|
|
|
|
2011-05-17 22:03:19 +04:00
|
|
|
# Raises a TypeError to prevent cloning.
|
2003-08-02 22:05:02 +04:00
|
|
|
def clone
|
|
|
|
raise TypeError, "can't clone instance of singleton #{self.class}"
|
|
|
|
end
|
2011-05-17 22:03:19 +04:00
|
|
|
|
|
|
|
# Raises a TypeError to prevent duping.
|
2003-08-02 22:05:02 +04:00
|
|
|
def dup
|
|
|
|
raise TypeError, "can't dup instance of singleton #{self.class}"
|
|
|
|
end
|
2008-05-28 10:18:18 +04:00
|
|
|
|
2011-05-17 22:03:19 +04:00
|
|
|
# By default, do not retain any state when marshalling.
|
2008-05-28 10:18:18 +04:00
|
|
|
def _dump(depth = -1)
|
2003-08-02 22:05:02 +04:00
|
|
|
''
|
|
|
|
end
|
|
|
|
|
2011-05-17 22:03:19 +04:00
|
|
|
module SingletonClassMethods # :nodoc:
|
|
|
|
|
|
|
|
def clone # :nodoc:
|
2023-06-05 04:09:52 +03:00
|
|
|
Singleton.__init__(super)
|
2003-08-02 22:05:02 +04:00
|
|
|
end
|
2006-12-31 18:02:22 +03:00
|
|
|
|
2011-05-17 22:03:19 +04:00
|
|
|
# By default calls instance(). Override to retain singleton state.
|
2008-05-28 10:18:18 +04:00
|
|
|
def _load(str)
|
|
|
|
instance
|
|
|
|
end
|
|
|
|
|
2019-06-28 08:23:22 +03:00
|
|
|
def instance # :nodoc:
|
2023-06-05 07:29:32 +03:00
|
|
|
@singleton__instance__ || @singleton__mutex__.synchronize { @singleton__instance__ ||= new }
|
2019-06-28 08:23:22 +03:00
|
|
|
end
|
|
|
|
|
2003-08-02 22:05:02 +04:00
|
|
|
private
|
2008-05-28 10:18:18 +04:00
|
|
|
|
2003-08-02 22:05:02 +04:00
|
|
|
def inherited(sub_klass)
|
|
|
|
super
|
2023-06-05 04:09:52 +03:00
|
|
|
Singleton.__init__(sub_klass)
|
2003-08-02 22:05:02 +04:00
|
|
|
end
|
2006-12-31 18:02:22 +03:00
|
|
|
end
|
|
|
|
|
2011-05-17 22:03:19 +04:00
|
|
|
class << Singleton # :nodoc:
|
2023-06-05 04:09:52 +03:00
|
|
|
def __init__(klass) # :nodoc:
|
|
|
|
klass.instance_eval {
|
|
|
|
@singleton__instance__ = nil
|
|
|
|
@singleton__mutex__ = Thread::Mutex.new
|
|
|
|
}
|
|
|
|
klass
|
|
|
|
end
|
|
|
|
|
2006-12-31 18:02:22 +03:00
|
|
|
private
|
|
|
|
|
2011-05-17 22:03:19 +04:00
|
|
|
# extending an object with Singleton is a bad idea
|
2006-12-31 18:02:22 +03:00
|
|
|
undef_method :extend_object
|
2008-05-28 10:18:18 +04:00
|
|
|
|
2006-12-31 18:02:22 +03: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 22:05:02 +04:00
|
|
|
end
|
2008-05-28 10:18:18 +04:00
|
|
|
|
2006-12-31 18:02:22 +03:00
|
|
|
def included(klass)
|
|
|
|
super
|
2011-05-17 22:03:19 +04:00
|
|
|
klass.private_class_method :new, :allocate
|
2006-12-31 18:02:22 +03:00
|
|
|
klass.extend SingletonClassMethods
|
2023-06-05 04:09:52 +03:00
|
|
|
Singleton.__init__(klass)
|
2003-08-02 22:05:02 +04:00
|
|
|
end
|
|
|
|
end
|
2008-05-28 10:18:18 +04:00
|
|
|
|
2011-05-17 22:03:19 +04:00
|
|
|
##
|
|
|
|
# :singleton-method: _load
|
|
|
|
# By default calls instance(). Override to retain singleton state.
|
2019-06-28 08:37:52 +03:00
|
|
|
|
|
|
|
##
|
|
|
|
# :singleton-method: instance
|
|
|
|
# Returns the singleton instance.
|
1999-08-13 09:45:20 +04:00
|
|
|
end
|