singleton 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b9dfa8197e311e8c51f00bf57763bb8b50310208e6a99a7baa3a60c874620956
4
- data.tar.gz: 68f60f4bcdcd68a06c39f72a1960a29a7c378a1e14ec419fcbcfa9c51e811399
3
+ metadata.gz: e4a0824cb3d773a47e6253026f7ed1a64838b6d83318e6c30632c9257fc1f7e7
4
+ data.tar.gz: 36a4eafc00c5cfa9d96d25fe73ec99631d47f0a37354aa65d671ca29f3ee033a
5
5
  SHA512:
6
- metadata.gz: b0160a2ea66e1a2a8f05a0f49bf03be28a0b5bb05999014006855078dfc2143cab53f33ac686fb39dbe4de1318b60873526239d0cc802f6700064d206dffdfe0
7
- data.tar.gz: 489e9e5984d81dc8e7bc848d308c5bfd82774ea88e13cc26ef2e8daa8ac23d7928ab562adcad1b0d92e74e2b1ae8613f94d86a7f9ffe1fa73c0fba9e64cf3619
6
+ metadata.gz: ee3e7eda2525837a6019328e6fa27b5745090737952b16c538d5702f74d9146d47d8d634787689dfcf5d9fb108570ac5218e518522eca4bc01b150ce4d6c253b
7
+ data.tar.gz: b2a588e68ae7bf836ff14de1c849f6c0fb7d1af0408174cb6d4ec0023e500f0f1fa348fc1d87d4142f455f0417ce4baafe7664f2510e420a53bf2a82d2eda056
@@ -0,0 +1,46 @@
1
+ name: Publish gem to rubygems.org
2
+
3
+ on:
4
+ push:
5
+ tags:
6
+ - 'v*'
7
+
8
+ permissions:
9
+ contents: read
10
+
11
+ jobs:
12
+ push:
13
+ if: github.repository == 'ruby/singleton'
14
+ runs-on: ubuntu-latest
15
+
16
+ environment:
17
+ name: rubygems.org
18
+ url: https://rubygems.org/gems/singleton
19
+
20
+ permissions:
21
+ contents: write
22
+ id-token: write
23
+
24
+ steps:
25
+ - name: Harden Runner
26
+ uses: step-security/harden-runner@91182cccc01eb5e619899d80e4e971d6181294a7 # v2.10.1
27
+ with:
28
+ egress-policy: audit
29
+
30
+ - uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4.1.4
31
+
32
+ - name: Set up Ruby
33
+ uses: ruby/setup-ruby@a6e6f86333f0a2523ece813039b8b4be04560854 # v1.190.0
34
+ with:
35
+ bundler-cache: true
36
+ ruby-version: ruby
37
+
38
+ - name: Publish to RubyGems
39
+ uses: rubygems/release-gem@612653d273a73bdae1df8453e090060bb4db5f31 # v1
40
+
41
+ - name: Create GitHub release
42
+ run: |
43
+ tag_name="$(git describe --tags --abbrev=0)"
44
+ gh release create "${tag_name}" --verify-tag --generate-notes
45
+ env:
46
+ GITHUB_TOKEN: ${{ secrets.MATZBOT_GITHUB_WORKFLOW_TOKEN }}
@@ -6,15 +6,20 @@ jobs:
6
6
  ruby-versions:
7
7
  uses: ruby/actions/.github/workflows/ruby_versions.yml@master
8
8
  with:
9
- engine: cruby
10
9
  min_version: 2.4
11
10
  test:
12
11
  needs: ruby-versions
13
12
  name: build (${{ matrix.ruby }} / ${{ matrix.os }})
14
13
  strategy:
14
+ fail-fast: false
15
15
  matrix:
16
16
  ruby: ${{ fromJson(needs.ruby-versions.outputs.versions) }}
17
- os: [ ubuntu-latest, macos-latest ]
17
+ os: [ ubuntu-latest, macos-latest, windows-latest ]
18
+ exclude:
19
+ - { os: macos-latest, ruby: 2.4 }
20
+ - { os: macos-latest, ruby: 2.5 }
21
+ - { os: windows-latest, ruby: truffleruby }
22
+ - { os: windows-latest, ruby: truffleruby-head }
18
23
  runs-on: ${{ matrix.os }}
19
24
  steps:
20
25
  - uses: actions/checkout@v4
@@ -4,10 +4,10 @@ Redistribution and use in source and binary forms, with or without
4
4
  modification, are permitted provided that the following conditions
5
5
  are met:
6
6
  1. Redistributions of source code must retain the above copyright
7
- notice, this list of conditions and the following disclaimer.
7
+ notice, this list of conditions and the following disclaimer.
8
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.
9
+ notice, this list of conditions and the following disclaimer in the
10
+ documentation and/or other materials provided with the distribution.
11
11
 
12
12
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
13
13
  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
data/COPYING ADDED
@@ -0,0 +1,56 @@
1
+ Ruby is copyrighted free software by Yukihiro Matsumoto <matz@netlab.jp>.
2
+ You can redistribute it and/or modify it under either the terms of the
3
+ 2-clause BSDL (see the file BSDL), or the conditions below:
4
+
5
+ 1. You may make and give away verbatim copies of the source form of the
6
+ software without restriction, provided that you duplicate all of the
7
+ original copyright notices and associated disclaimers.
8
+
9
+ 2. You may modify your copy of the software in any way, provided that
10
+ you do at least ONE of the following:
11
+
12
+ a. place your modifications in the Public Domain or otherwise
13
+ make them Freely Available, such as by posting said
14
+ modifications to Usenet or an equivalent medium, or by allowing
15
+ the author to include your modifications in the software.
16
+
17
+ b. use the modified software only within your corporation or
18
+ organization.
19
+
20
+ c. give non-standard binaries non-standard names, with
21
+ instructions on where to get the original software distribution.
22
+
23
+ d. make other distribution arrangements with the author.
24
+
25
+ 3. You may distribute the software in object code or binary form,
26
+ provided that you do at least ONE of the following:
27
+
28
+ a. distribute the binaries and library files of the software,
29
+ together with instructions (in the manual page or equivalent)
30
+ on where to get the original distribution.
31
+
32
+ b. accompany the distribution with the machine-readable source of
33
+ the software.
34
+
35
+ c. give non-standard binaries non-standard names, with
36
+ instructions on where to get the original software distribution.
37
+
38
+ d. make other distribution arrangements with the author.
39
+
40
+ 4. You may modify and include the part of the software into any other
41
+ software (possibly commercial). But some files in the distribution
42
+ are not written by the author, so that they are not under these terms.
43
+
44
+ For the list of those files and their copying conditions, see the
45
+ file LEGAL.
46
+
47
+ 5. The scripts and library files supplied as input to or produced as
48
+ output from the software do not automatically fall under the
49
+ copyright of the software, but belong to whomever generated them,
50
+ and may be sold commercially, and may be aggregated with this
51
+ software.
52
+
53
+ 6. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
54
+ IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
55
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
56
+ PURPOSE.
data/lib/singleton.rb CHANGED
@@ -1,4 +1,4 @@
1
- # frozen_string_literal: false
1
+ # frozen_string_literal: true
2
2
 
3
3
  # The Singleton module implements the Singleton pattern.
4
4
  #
@@ -92,22 +92,25 @@
92
92
  # p a.strip # => nil
93
93
  #
94
94
  module Singleton
95
- VERSION = "0.2.0"
95
+ VERSION = "0.3.0"
96
96
 
97
- # Raises a TypeError to prevent cloning.
98
- def clone
99
- raise TypeError, "can't clone instance of singleton #{self.class}"
100
- end
97
+ module SingletonInstanceMethods
98
+ # Raises a TypeError to prevent cloning.
99
+ def clone
100
+ raise TypeError, "can't clone instance of singleton #{self.class}"
101
+ end
101
102
 
102
- # Raises a TypeError to prevent duping.
103
- def dup
104
- raise TypeError, "can't dup instance of singleton #{self.class}"
105
- end
103
+ # Raises a TypeError to prevent duping.
104
+ def dup
105
+ raise TypeError, "can't dup instance of singleton #{self.class}"
106
+ end
106
107
 
107
- # By default, do not retain any state when marshalling.
108
- def _dump(depth = -1)
109
- ''
108
+ # By default, do not retain any state when marshalling.
109
+ def _dump(depth = -1)
110
+ ''
111
+ end
110
112
  end
113
+ include SingletonInstanceMethods
111
114
 
112
115
  module SingletonClassMethods # :nodoc:
113
116
 
@@ -130,22 +133,42 @@ module Singleton
130
133
  super
131
134
  Singleton.__init__(sub_klass)
132
135
  end
136
+
137
+ def set_instance(val)
138
+ @singleton__instance__ = val
139
+ end
140
+
141
+ def set_mutex(val)
142
+ @singleton__mutex__ = val
143
+ end
133
144
  end
134
145
 
135
- class << Singleton # :nodoc:
146
+ def self.module_with_class_methods
147
+ SingletonClassMethods
148
+ end
149
+
150
+ module SingletonClassProperties
151
+
152
+ def self.included(c)
153
+ # extending an object with Singleton is a bad idea
154
+ c.undef_method :extend_object
155
+ end
156
+
157
+ def self.extended(c)
158
+ # extending an object with Singleton is a bad idea
159
+ c.singleton_class.send(:undef_method, :extend_object)
160
+ end
161
+
136
162
  def __init__(klass) # :nodoc:
137
163
  klass.instance_eval {
138
- @singleton__instance__ = nil
139
- @singleton__mutex__ = Thread::Mutex.new
164
+ set_instance(nil)
165
+ set_mutex(Thread::Mutex.new)
140
166
  }
141
167
  klass
142
168
  end
143
169
 
144
170
  private
145
171
 
146
- # extending an object with Singleton is a bad idea
147
- undef_method :extend_object
148
-
149
172
  def append_features(mod)
150
173
  # help out people counting on transitive mixins
151
174
  unless mod.instance_of?(Class)
@@ -157,10 +180,11 @@ module Singleton
157
180
  def included(klass)
158
181
  super
159
182
  klass.private_class_method :new, :allocate
160
- klass.extend SingletonClassMethods
183
+ klass.extend module_with_class_methods
161
184
  Singleton.__init__(klass)
162
185
  end
163
186
  end
187
+ extend SingletonClassProperties
164
188
 
165
189
  ##
166
190
  # :singleton-method: _load
@@ -170,3 +194,46 @@ module Singleton
170
194
  # :singleton-method: instance
171
195
  # Returns the singleton instance.
172
196
  end
197
+
198
+ if defined?(Ractor)
199
+ module RactorLocalSingleton
200
+ include Singleton::SingletonInstanceMethods
201
+
202
+ module RactorLocalSingletonClassMethods
203
+ include Singleton::SingletonClassMethods
204
+ def instance
205
+ set_mutex(Thread::Mutex.new) if Ractor.current[mutex_key].nil?
206
+ return Ractor.current[instance_key] if Ractor.current[instance_key]
207
+ Ractor.current[mutex_key].synchronize {
208
+ return Ractor.current[instance_key] if Ractor.current[instance_key]
209
+ set_instance(new())
210
+ }
211
+ Ractor.current[instance_key]
212
+ end
213
+
214
+ private
215
+
216
+ def instance_key
217
+ :"__RactorLocalSingleton_instance_with_class_id_#{object_id}__"
218
+ end
219
+
220
+ def mutex_key
221
+ :"__RactorLocalSingleton_mutex_with_class_id_#{object_id}__"
222
+ end
223
+
224
+ def set_instance(val)
225
+ Ractor.current[instance_key] = val
226
+ end
227
+
228
+ def set_mutex(val)
229
+ Ractor.current[mutex_key] = val
230
+ end
231
+ end
232
+
233
+ def self.module_with_class_methods
234
+ RactorLocalSingletonClassMethods
235
+ end
236
+
237
+ extend Singleton::SingletonClassProperties
238
+ end
239
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: singleton
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yukihiro Matsumoto
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-11-06 00:00:00.000000000 Z
11
+ date: 2024-11-08 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: The Singleton module implements the Singleton pattern.
14
14
  email:
@@ -18,10 +18,12 @@ extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
20
  - ".github/dependabot.yml"
21
+ - ".github/workflows/push_gem.yml"
21
22
  - ".github/workflows/test.yml"
22
23
  - ".gitignore"
24
+ - BSDL
25
+ - COPYING
23
26
  - Gemfile
24
- - LICENSE.txt
25
27
  - README.md
26
28
  - Rakefile
27
29
  - bin/console
@@ -35,7 +37,7 @@ licenses:
35
37
  metadata:
36
38
  homepage_uri: https://github.com/ruby/singleton
37
39
  source_code_uri: https://github.com/ruby/singleton
38
- post_install_message:
40
+ post_install_message:
39
41
  rdoc_options: []
40
42
  require_paths:
41
43
  - lib
@@ -50,8 +52,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
50
52
  - !ruby/object:Gem::Version
51
53
  version: '0'
52
54
  requirements: []
53
- rubygems_version: 3.5.0.dev
54
- signing_key:
55
+ rubygems_version: 3.5.11
56
+ signing_key:
55
57
  specification_version: 4
56
58
  summary: The Singleton module implements the Singleton pattern.
57
59
  test_files: []