singleton 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.travis.yml +7 -0
- data/Gemfile +9 -0
- data/LICENSE.txt +22 -0
- data/README.md +70 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/singleton.rb +177 -0
- data/lib/singleton/version.rb +3 -0
- data/singleton.gemspec +25 -0
- metadata +56 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 69ce5ea038b9a035ac62e61824f08b9e6d806e26309f5666b6bdbd778baeb385
|
4
|
+
data.tar.gz: 02e3504ffb6547ce784ccd4579f4b4e2cd0f0deddedc228f3c390bfee202240f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 80e0df606115bfde6d963d4511664c1862d924dd3f6678a1368cd12290c144dc43ede2a62f3abd3bf8be294d9e72e99f0da42611aa9e0c9c298141acd426faf3
|
7
|
+
data.tar.gz: e3a735efe724e487dc4f032c2b0f67a9fc1b2fd2840ff8c5840ffedd46c0889906e213a6946dc27f2bdcedab72e9067a1f188de55b003d8c34216781eca1318f
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (C) 1993-2013 Yukihiro Matsumoto. All rights reserved.
|
2
|
+
|
3
|
+
Redistribution and use in source and binary forms, with or without
|
4
|
+
modification, are permitted provided that the following conditions
|
5
|
+
are met:
|
6
|
+
1. Redistributions of source code must retain the above copyright
|
7
|
+
notice, this list of conditions and the following disclaimer.
|
8
|
+
2. Redistributions in binary form must reproduce the above copyright
|
9
|
+
notice, this list of conditions and the following disclaimer in the
|
10
|
+
documentation and/or other materials provided with the distribution.
|
11
|
+
|
12
|
+
THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
13
|
+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
14
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
15
|
+
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
16
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
17
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
18
|
+
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
19
|
+
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
20
|
+
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
21
|
+
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
22
|
+
SUCH DAMAGE.
|
data/README.md
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
# Singleton
|
2
|
+
|
3
|
+
The Singleton module implements the Singleton pattern.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'singleton'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install singleton
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
To use Singleton, include the module in your class.
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
class Klass
|
27
|
+
include Singleton
|
28
|
+
# ...
|
29
|
+
end
|
30
|
+
```
|
31
|
+
|
32
|
+
This ensures that only one instance of Klass can be created.
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
a,b = Klass.instance, Klass.instance
|
36
|
+
|
37
|
+
a == b
|
38
|
+
# => true
|
39
|
+
|
40
|
+
Klass.new
|
41
|
+
# => NoMethodError - new is private ...
|
42
|
+
```
|
43
|
+
|
44
|
+
The instance is created at upon the first call of Klass.instance().
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
class OtherKlass
|
48
|
+
include Singleton
|
49
|
+
# ...
|
50
|
+
end
|
51
|
+
|
52
|
+
ObjectSpace.each_object(OtherKlass){}
|
53
|
+
# => 0
|
54
|
+
|
55
|
+
OtherKlass.instance
|
56
|
+
ObjectSpace.each_object(OtherKlass){}
|
57
|
+
# => 1
|
58
|
+
```
|
59
|
+
|
60
|
+
This behavior is preserved under inheritance and cloning.
|
61
|
+
|
62
|
+
## Development
|
63
|
+
|
64
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
65
|
+
|
66
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
67
|
+
|
68
|
+
## Contributing
|
69
|
+
|
70
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/ruby/singleton.
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "singleton"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/singleton.rb
ADDED
@@ -0,0 +1,177 @@
|
|
1
|
+
# frozen_string_literal: false
|
2
|
+
|
3
|
+
# The Singleton module implements the Singleton pattern.
|
4
|
+
#
|
5
|
+
# == Usage
|
6
|
+
#
|
7
|
+
# To use Singleton, include the module in your class.
|
8
|
+
#
|
9
|
+
# class Klass
|
10
|
+
# include Singleton
|
11
|
+
# # ...
|
12
|
+
# end
|
13
|
+
#
|
14
|
+
# This ensures that only one instance of Klass can be created.
|
15
|
+
#
|
16
|
+
# a,b = Klass.instance, Klass.instance
|
17
|
+
#
|
18
|
+
# a == b
|
19
|
+
# # => true
|
20
|
+
#
|
21
|
+
# Klass.new
|
22
|
+
# # => NoMethodError - new is private ...
|
23
|
+
#
|
24
|
+
# The instance is created at upon the first call of Klass.instance().
|
25
|
+
#
|
26
|
+
# class OtherKlass
|
27
|
+
# include Singleton
|
28
|
+
# # ...
|
29
|
+
# end
|
30
|
+
#
|
31
|
+
# ObjectSpace.each_object(OtherKlass){}
|
32
|
+
# # => 0
|
33
|
+
#
|
34
|
+
# OtherKlass.instance
|
35
|
+
# ObjectSpace.each_object(OtherKlass){}
|
36
|
+
# # => 1
|
37
|
+
#
|
38
|
+
#
|
39
|
+
# This behavior is preserved under inheritance and cloning.
|
40
|
+
#
|
41
|
+
# == Implementation
|
42
|
+
#
|
43
|
+
# This above is achieved by:
|
44
|
+
#
|
45
|
+
# * Making Klass.new and Klass.allocate private.
|
46
|
+
#
|
47
|
+
# * Overriding Klass.inherited(sub_klass) and Klass.clone() to ensure that the
|
48
|
+
# Singleton properties are kept when inherited and cloned.
|
49
|
+
#
|
50
|
+
# * Providing the Klass.instance() method that returns the same object each
|
51
|
+
# time it is called.
|
52
|
+
#
|
53
|
+
# * Overriding Klass._load(str) to call Klass.instance().
|
54
|
+
#
|
55
|
+
# * Overriding Klass#clone and Klass#dup to raise TypeErrors to prevent
|
56
|
+
# cloning or duping.
|
57
|
+
#
|
58
|
+
# == Singleton and Marshal
|
59
|
+
#
|
60
|
+
# By default Singleton's #_dump(depth) returns the empty string. Marshalling by
|
61
|
+
# default will strip state information, e.g. instance variables and taint
|
62
|
+
# state, from the instance. Classes using Singleton can provide custom
|
63
|
+
# _load(str) and _dump(depth) methods to retain some of the previous state of
|
64
|
+
# the instance.
|
65
|
+
#
|
66
|
+
# require 'singleton'
|
67
|
+
#
|
68
|
+
# class Example
|
69
|
+
# include Singleton
|
70
|
+
# attr_accessor :keep, :strip
|
71
|
+
# def _dump(depth)
|
72
|
+
# # this strips the @strip information from the instance
|
73
|
+
# Marshal.dump(@keep, depth)
|
74
|
+
# end
|
75
|
+
#
|
76
|
+
# def self._load(str)
|
77
|
+
# instance.keep = Marshal.load(str)
|
78
|
+
# instance
|
79
|
+
# end
|
80
|
+
# end
|
81
|
+
#
|
82
|
+
# a = Example.instance
|
83
|
+
# a.keep = "keep this"
|
84
|
+
# a.strip = "get rid of this"
|
85
|
+
# a.taint
|
86
|
+
#
|
87
|
+
# stored_state = Marshal.dump(a)
|
88
|
+
#
|
89
|
+
# a.keep = nil
|
90
|
+
# a.strip = nil
|
91
|
+
# b = Marshal.load(stored_state)
|
92
|
+
# p a == b # => true
|
93
|
+
# p a.keep # => "keep this"
|
94
|
+
# p a.strip # => nil
|
95
|
+
#
|
96
|
+
module Singleton
|
97
|
+
# Raises a TypeError to prevent cloning.
|
98
|
+
def clone
|
99
|
+
raise TypeError, "can't clone instance of singleton #{self.class}"
|
100
|
+
end
|
101
|
+
|
102
|
+
# Raises a TypeError to prevent duping.
|
103
|
+
def dup
|
104
|
+
raise TypeError, "can't dup instance of singleton #{self.class}"
|
105
|
+
end
|
106
|
+
|
107
|
+
# By default, do not retain any state when marshalling.
|
108
|
+
def _dump(depth = -1)
|
109
|
+
''
|
110
|
+
end
|
111
|
+
|
112
|
+
module SingletonClassMethods # :nodoc:
|
113
|
+
|
114
|
+
def clone # :nodoc:
|
115
|
+
Singleton.__init__(super)
|
116
|
+
end
|
117
|
+
|
118
|
+
# By default calls instance(). Override to retain singleton state.
|
119
|
+
def _load(str)
|
120
|
+
instance
|
121
|
+
end
|
122
|
+
|
123
|
+
def instance # :nodoc:
|
124
|
+
return @singleton__instance__ if @singleton__instance__
|
125
|
+
@singleton__mutex__.synchronize {
|
126
|
+
return @singleton__instance__ if @singleton__instance__
|
127
|
+
@singleton__instance__ = new()
|
128
|
+
}
|
129
|
+
@singleton__instance__
|
130
|
+
end
|
131
|
+
|
132
|
+
private
|
133
|
+
|
134
|
+
def inherited(sub_klass)
|
135
|
+
super
|
136
|
+
Singleton.__init__(sub_klass)
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
class << Singleton # :nodoc:
|
141
|
+
def __init__(klass) # :nodoc:
|
142
|
+
klass.instance_eval {
|
143
|
+
@singleton__instance__ = nil
|
144
|
+
@singleton__mutex__ = Thread::Mutex.new
|
145
|
+
}
|
146
|
+
klass
|
147
|
+
end
|
148
|
+
|
149
|
+
private
|
150
|
+
|
151
|
+
# extending an object with Singleton is a bad idea
|
152
|
+
undef_method :extend_object
|
153
|
+
|
154
|
+
def append_features(mod)
|
155
|
+
# help out people counting on transitive mixins
|
156
|
+
unless mod.instance_of?(Class)
|
157
|
+
raise TypeError, "Inclusion of the OO-Singleton module in module #{mod}"
|
158
|
+
end
|
159
|
+
super
|
160
|
+
end
|
161
|
+
|
162
|
+
def included(klass)
|
163
|
+
super
|
164
|
+
klass.private_class_method :new, :allocate
|
165
|
+
klass.extend SingletonClassMethods
|
166
|
+
Singleton.__init__(klass)
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
##
|
171
|
+
# :singleton-method: _load
|
172
|
+
# By default calls instance(). Override to retain singleton state.
|
173
|
+
|
174
|
+
##
|
175
|
+
# :singleton-method: instance
|
176
|
+
# Returns the singleton instance.
|
177
|
+
end
|
data/singleton.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
lib = File.expand_path("lib", __dir__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require "singleton/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "singleton"
|
7
|
+
spec.version = Singleton::VERSION
|
8
|
+
spec.authors = ["Yukihiro Matsumoto"]
|
9
|
+
spec.email = ["matz@ruby-lang.org"]
|
10
|
+
|
11
|
+
spec.summary = %q{The Singleton module implements the Singleton pattern.}
|
12
|
+
spec.description = spec.summary
|
13
|
+
spec.homepage = "https://github.com/ruby/singleton"
|
14
|
+
spec.license = "BSD-2-Clause"
|
15
|
+
|
16
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
17
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
18
|
+
|
19
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
20
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
21
|
+
end
|
22
|
+
spec.bindir = "exe"
|
23
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
24
|
+
spec.require_paths = ["lib"]
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: singleton
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Yukihiro Matsumoto
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-11-06 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: The Singleton module implements the Singleton pattern.
|
14
|
+
email:
|
15
|
+
- matz@ruby-lang.org
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ".gitignore"
|
21
|
+
- ".travis.yml"
|
22
|
+
- Gemfile
|
23
|
+
- LICENSE.txt
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- bin/console
|
27
|
+
- bin/setup
|
28
|
+
- lib/singleton.rb
|
29
|
+
- lib/singleton/version.rb
|
30
|
+
- singleton.gemspec
|
31
|
+
homepage: https://github.com/ruby/singleton
|
32
|
+
licenses:
|
33
|
+
- BSD-2-Clause
|
34
|
+
metadata:
|
35
|
+
homepage_uri: https://github.com/ruby/singleton
|
36
|
+
source_code_uri: https://github.com/ruby/singleton
|
37
|
+
post_install_message:
|
38
|
+
rdoc_options: []
|
39
|
+
require_paths:
|
40
|
+
- lib
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '0'
|
51
|
+
requirements: []
|
52
|
+
rubygems_version: 3.0.3
|
53
|
+
signing_key:
|
54
|
+
specification_version: 4
|
55
|
+
summary: The Singleton module implements the Singleton pattern.
|
56
|
+
test_files: []
|