Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/smanonr..../opt/alt/ruby27/share/ruby
File: singleton.rb
# frozen_string_literal: false
[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 from the instance.
[60] Fix | Delete
# Classes using Singleton can provide custom _load(str) and _dump(depth) methods
[61] Fix | Delete
# to retain some of the previous state of the instance.
[62] Fix | Delete
#
[63] Fix | Delete
# require 'singleton'
[64] Fix | Delete
#
[65] Fix | Delete
# class Example
[66] Fix | Delete
# include Singleton
[67] Fix | Delete
# attr_accessor :keep, :strip
[68] Fix | Delete
# def _dump(depth)
[69] Fix | Delete
# # this strips the @strip information from the instance
[70] Fix | Delete
# Marshal.dump(@keep, depth)
[71] Fix | Delete
# end
[72] Fix | Delete
#
[73] Fix | Delete
# def self._load(str)
[74] Fix | Delete
# instance.keep = Marshal.load(str)
[75] Fix | Delete
# instance
[76] Fix | Delete
# end
[77] Fix | Delete
# end
[78] Fix | Delete
#
[79] Fix | Delete
# a = Example.instance
[80] Fix | Delete
# a.keep = "keep this"
[81] Fix | Delete
# a.strip = "get rid of this"
[82] Fix | Delete
#
[83] Fix | Delete
# stored_state = Marshal.dump(a)
[84] Fix | Delete
#
[85] Fix | Delete
# a.keep = nil
[86] Fix | Delete
# a.strip = nil
[87] Fix | Delete
# b = Marshal.load(stored_state)
[88] Fix | Delete
# p a == b # => true
[89] Fix | Delete
# p a.keep # => "keep this"
[90] Fix | Delete
# p a.strip # => nil
[91] Fix | Delete
#
[92] Fix | Delete
module Singleton
[93] Fix | Delete
# Raises a TypeError to prevent cloning.
[94] Fix | Delete
def clone
[95] Fix | Delete
raise TypeError, "can't clone instance of singleton #{self.class}"
[96] Fix | Delete
end
[97] Fix | Delete
[98] Fix | Delete
# Raises a TypeError to prevent duping.
[99] Fix | Delete
def dup
[100] Fix | Delete
raise TypeError, "can't dup instance of singleton #{self.class}"
[101] Fix | Delete
end
[102] Fix | Delete
[103] Fix | Delete
# By default, do not retain any state when marshalling.
[104] Fix | Delete
def _dump(depth = -1)
[105] Fix | Delete
''
[106] Fix | Delete
end
[107] Fix | Delete
[108] Fix | Delete
module SingletonClassMethods # :nodoc:
[109] Fix | Delete
[110] Fix | Delete
def clone # :nodoc:
[111] Fix | Delete
Singleton.__init__(super)
[112] Fix | Delete
end
[113] Fix | Delete
[114] Fix | Delete
# By default calls instance(). Override to retain singleton state.
[115] Fix | Delete
def _load(str)
[116] Fix | Delete
instance
[117] Fix | Delete
end
[118] Fix | Delete
[119] Fix | Delete
def instance # :nodoc:
[120] Fix | Delete
return @singleton__instance__ if @singleton__instance__
[121] Fix | Delete
@singleton__mutex__.synchronize {
[122] Fix | Delete
return @singleton__instance__ if @singleton__instance__
[123] Fix | Delete
@singleton__instance__ = new()
[124] Fix | Delete
}
[125] Fix | Delete
@singleton__instance__
[126] Fix | Delete
end
[127] Fix | Delete
[128] Fix | Delete
private
[129] Fix | Delete
[130] Fix | Delete
def inherited(sub_klass)
[131] Fix | Delete
super
[132] Fix | Delete
Singleton.__init__(sub_klass)
[133] Fix | Delete
end
[134] Fix | Delete
end
[135] Fix | Delete
[136] Fix | Delete
class << Singleton # :nodoc:
[137] Fix | Delete
def __init__(klass) # :nodoc:
[138] Fix | Delete
klass.instance_eval {
[139] Fix | Delete
@singleton__instance__ = nil
[140] Fix | Delete
@singleton__mutex__ = Thread::Mutex.new
[141] Fix | Delete
}
[142] Fix | Delete
klass
[143] Fix | Delete
end
[144] Fix | Delete
[145] Fix | Delete
private
[146] Fix | Delete
[147] Fix | Delete
# extending an object with Singleton is a bad idea
[148] Fix | Delete
undef_method :extend_object
[149] Fix | Delete
[150] Fix | Delete
def append_features(mod)
[151] Fix | Delete
# help out people counting on transitive mixins
[152] Fix | Delete
unless mod.instance_of?(Class)
[153] Fix | Delete
raise TypeError, "Inclusion of the OO-Singleton module in module #{mod}"
[154] Fix | Delete
end
[155] Fix | Delete
super
[156] Fix | Delete
end
[157] Fix | Delete
[158] Fix | Delete
def included(klass)
[159] Fix | Delete
super
[160] Fix | Delete
klass.private_class_method :new, :allocate
[161] Fix | Delete
klass.extend SingletonClassMethods
[162] Fix | Delete
Singleton.__init__(klass)
[163] Fix | Delete
end
[164] Fix | Delete
end
[165] Fix | Delete
[166] Fix | Delete
##
[167] Fix | Delete
# :singleton-method: _load
[168] Fix | Delete
# By default calls instance(). Override to retain singleton state.
[169] Fix | Delete
[170] Fix | Delete
##
[171] Fix | Delete
# :singleton-method: instance
[172] Fix | Delete
# Returns the singleton instance.
[173] Fix | Delete
end
[174] Fix | Delete
[175] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function