Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/smanonr..../opt/alt/ruby31/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
VERSION = "0.1.1"
[94] Fix | Delete
[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
def instance # :nodoc:
[122] Fix | Delete
return @singleton__instance__ if @singleton__instance__
[123] Fix | Delete
@singleton__mutex__.synchronize {
[124] Fix | Delete
return @singleton__instance__ if @singleton__instance__
[125] Fix | Delete
@singleton__instance__ = new()
[126] Fix | Delete
}
[127] Fix | Delete
@singleton__instance__
[128] Fix | Delete
end
[129] Fix | Delete
[130] Fix | Delete
private
[131] Fix | Delete
[132] Fix | Delete
def inherited(sub_klass)
[133] Fix | Delete
super
[134] Fix | Delete
Singleton.__init__(sub_klass)
[135] Fix | Delete
end
[136] Fix | Delete
end
[137] Fix | Delete
[138] Fix | Delete
class << Singleton # :nodoc:
[139] Fix | Delete
def __init__(klass) # :nodoc:
[140] Fix | Delete
klass.instance_eval {
[141] Fix | Delete
@singleton__instance__ = nil
[142] Fix | Delete
@singleton__mutex__ = Thread::Mutex.new
[143] Fix | Delete
}
[144] Fix | Delete
klass
[145] Fix | Delete
end
[146] Fix | Delete
[147] Fix | Delete
private
[148] Fix | Delete
[149] Fix | Delete
# extending an object with Singleton is a bad idea
[150] Fix | Delete
undef_method :extend_object
[151] Fix | Delete
[152] Fix | Delete
def append_features(mod)
[153] Fix | Delete
# help out people counting on transitive mixins
[154] Fix | Delete
unless mod.instance_of?(Class)
[155] Fix | Delete
raise TypeError, "Inclusion of the OO-Singleton module in module #{mod}"
[156] Fix | Delete
end
[157] Fix | Delete
super
[158] Fix | Delete
end
[159] Fix | Delete
[160] Fix | Delete
def included(klass)
[161] Fix | Delete
super
[162] Fix | Delete
klass.private_class_method :new, :allocate
[163] Fix | Delete
klass.extend SingletonClassMethods
[164] Fix | Delete
Singleton.__init__(klass)
[165] Fix | Delete
end
[166] Fix | Delete
end
[167] Fix | Delete
[168] Fix | Delete
##
[169] Fix | Delete
# :singleton-method: _load
[170] Fix | Delete
# By default calls instance(). Override to retain singleton state.
[171] Fix | Delete
[172] Fix | Delete
##
[173] Fix | Delete
# :singleton-method: instance
[174] Fix | Delete
# Returns the singleton instance.
[175] Fix | Delete
end
[176] Fix | Delete
[177] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function