Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/ExeBy/exe_root.../opt/alt/ruby22/lib64/ruby/2.2.0
File: singleton.rb
require 'thread'
[0] Fix | Delete
[1] Fix | Delete
# The Singleton module implements the Singleton pattern.
[2] Fix | Delete
#
[3] Fix | Delete
# == Usage
[4] Fix | Delete
#
[5] Fix | Delete
# To use Singleton, include the module in your class.
[6] Fix | Delete
#
[7] Fix | Delete
# class Klass
[8] Fix | Delete
# include Singleton
[9] Fix | Delete
# # ...
[10] Fix | Delete
# end
[11] Fix | Delete
#
[12] Fix | Delete
# This ensures that only one instance of Klass can be created.
[13] Fix | Delete
#
[14] Fix | Delete
# a,b = Klass.instance, Klass.instance
[15] Fix | Delete
#
[16] Fix | Delete
# a == b
[17] Fix | Delete
# # => true
[18] Fix | Delete
#
[19] Fix | Delete
# Klass.new
[20] Fix | Delete
# # => NoMethodError - new is private ...
[21] Fix | Delete
#
[22] Fix | Delete
# The instance is created at upon the first call of Klass.instance().
[23] Fix | Delete
#
[24] Fix | Delete
# class OtherKlass
[25] Fix | Delete
# include Singleton
[26] Fix | Delete
# # ...
[27] Fix | Delete
# end
[28] Fix | Delete
#
[29] Fix | Delete
# ObjectSpace.each_object(OtherKlass){}
[30] Fix | Delete
# # => 0
[31] Fix | Delete
#
[32] Fix | Delete
# OtherKlass.instance
[33] Fix | Delete
# ObjectSpace.each_object(OtherKlass){}
[34] Fix | Delete
# # => 1
[35] Fix | Delete
#
[36] Fix | Delete
#
[37] Fix | Delete
# This behavior is preserved under inheritance and cloning.
[38] Fix | Delete
#
[39] Fix | Delete
# == Implementation
[40] Fix | Delete
#
[41] Fix | Delete
# This above is achieved by:
[42] Fix | Delete
#
[43] Fix | Delete
# * Making Klass.new and Klass.allocate private.
[44] Fix | Delete
#
[45] Fix | Delete
# * Overriding Klass.inherited(sub_klass) and Klass.clone() to ensure that the
[46] Fix | Delete
# Singleton properties are kept when inherited and cloned.
[47] Fix | Delete
#
[48] Fix | Delete
# * Providing the Klass.instance() method that returns the same object each
[49] Fix | Delete
# time it is called.
[50] Fix | Delete
#
[51] Fix | Delete
# * Overriding Klass._load(str) to call Klass.instance().
[52] Fix | Delete
#
[53] Fix | Delete
# * Overriding Klass#clone and Klass#dup to raise TypeErrors to prevent
[54] Fix | Delete
# cloning or duping.
[55] Fix | Delete
#
[56] Fix | Delete
# == Singleton and Marshal
[57] Fix | Delete
#
[58] Fix | Delete
# By default Singleton's #_dump(depth) returns the empty string. Marshalling by
[59] Fix | Delete
# default will strip state information, e.g. instance variables and taint
[60] Fix | Delete
# state, from the instance. Classes using Singleton can provide custom
[61] Fix | Delete
# _load(str) and _dump(depth) methods to retain some of the previous state of
[62] Fix | Delete
# the instance.
[63] Fix | Delete
#
[64] Fix | Delete
# require 'singleton'
[65] Fix | Delete
#
[66] Fix | Delete
# class Example
[67] Fix | Delete
# include Singleton
[68] Fix | Delete
# attr_accessor :keep, :strip
[69] Fix | Delete
# def _dump(depth)
[70] Fix | Delete
# # this strips the @strip information from the instance
[71] Fix | Delete
# Marshal.dump(@keep, depth)
[72] Fix | Delete
# end
[73] Fix | Delete
#
[74] Fix | Delete
# def self._load(str)
[75] Fix | Delete
# instance.keep = Marshal.load(str)
[76] Fix | Delete
# instance
[77] Fix | Delete
# end
[78] Fix | Delete
# end
[79] Fix | Delete
#
[80] Fix | Delete
# a = Example.instance
[81] Fix | Delete
# a.keep = "keep this"
[82] Fix | Delete
# a.strip = "get rid of this"
[83] Fix | Delete
# a.taint
[84] Fix | Delete
#
[85] Fix | Delete
# stored_state = Marshal.dump(a)
[86] Fix | Delete
#
[87] Fix | Delete
# a.keep = nil
[88] Fix | Delete
# a.strip = nil
[89] Fix | Delete
# b = Marshal.load(stored_state)
[90] Fix | Delete
# p a == b # => true
[91] Fix | Delete
# p a.keep # => "keep this"
[92] Fix | Delete
# p a.strip # => nil
[93] Fix | Delete
#
[94] Fix | Delete
module Singleton
[95] Fix | Delete
# Raises a TypeError to prevent cloning.
[96] Fix | Delete
def clone
[97] Fix | Delete
raise TypeError, "can't clone instance of singleton #{self.class}"
[98] Fix | Delete
end
[99] Fix | Delete
[100] Fix | Delete
# Raises a TypeError to prevent duping.
[101] Fix | Delete
def dup
[102] Fix | Delete
raise TypeError, "can't dup instance of singleton #{self.class}"
[103] Fix | Delete
end
[104] Fix | Delete
[105] Fix | Delete
# By default, do not retain any state when marshalling.
[106] Fix | Delete
def _dump(depth = -1)
[107] Fix | Delete
''
[108] Fix | Delete
end
[109] Fix | Delete
[110] Fix | Delete
module SingletonClassMethods # :nodoc:
[111] Fix | Delete
[112] Fix | Delete
def clone # :nodoc:
[113] Fix | Delete
Singleton.__init__(super)
[114] Fix | Delete
end
[115] Fix | Delete
[116] Fix | Delete
# By default calls instance(). Override to retain singleton state.
[117] Fix | Delete
def _load(str)
[118] Fix | Delete
instance
[119] Fix | Delete
end
[120] Fix | Delete
[121] Fix | Delete
private
[122] Fix | Delete
[123] Fix | Delete
def inherited(sub_klass)
[124] Fix | Delete
super
[125] Fix | Delete
Singleton.__init__(sub_klass)
[126] Fix | Delete
end
[127] Fix | Delete
end
[128] Fix | Delete
[129] Fix | Delete
class << Singleton # :nodoc:
[130] Fix | Delete
def __init__(klass) # :nodoc:
[131] Fix | Delete
klass.instance_eval {
[132] Fix | Delete
@singleton__instance__ = nil
[133] Fix | Delete
@singleton__mutex__ = Mutex.new
[134] Fix | Delete
}
[135] Fix | Delete
def klass.instance # :nodoc:
[136] Fix | Delete
return @singleton__instance__ if @singleton__instance__
[137] Fix | Delete
@singleton__mutex__.synchronize {
[138] Fix | Delete
return @singleton__instance__ if @singleton__instance__
[139] Fix | Delete
@singleton__instance__ = new()
[140] Fix | Delete
}
[141] Fix | Delete
@singleton__instance__
[142] Fix | Delete
end
[143] Fix | Delete
klass
[144] Fix | Delete
end
[145] Fix | Delete
[146] Fix | Delete
private
[147] Fix | Delete
[148] Fix | Delete
# extending an object with Singleton is a bad idea
[149] Fix | Delete
undef_method :extend_object
[150] Fix | Delete
[151] Fix | Delete
def append_features(mod)
[152] Fix | Delete
# help out people counting on transitive mixins
[153] Fix | Delete
unless mod.instance_of?(Class)
[154] Fix | Delete
raise TypeError, "Inclusion of the OO-Singleton module in module #{mod}"
[155] Fix | Delete
end
[156] Fix | Delete
super
[157] Fix | Delete
end
[158] Fix | Delete
[159] Fix | Delete
def included(klass)
[160] Fix | Delete
super
[161] Fix | Delete
klass.private_class_method :new, :allocate
[162] Fix | Delete
klass.extend SingletonClassMethods
[163] Fix | Delete
Singleton.__init__(klass)
[164] Fix | Delete
end
[165] Fix | Delete
end
[166] Fix | Delete
[167] Fix | Delete
##
[168] Fix | Delete
# :singleton-method: _load
[169] Fix | Delete
# By default calls instance(). Override to retain singleton state.
[170] Fix | Delete
end
[171] Fix | Delete
[172] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function