weakref 0.1.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b3dcc36ac8d61a70a118eb1adbfe9667347be766be52ec6096d6295e08e00769
4
+ data.tar.gz: fae5525d82ebf453b7154dac191738d726848d063705ac307784bea63e4d9a4a
5
+ SHA512:
6
+ metadata.gz: a50aec393b92017ab61248c8ac7cce139c10fc68eaae4d8d83ce87537b75d0f5de3e152f5e6e45597f00b74c0adaf72858e1f04092d98093fcaade8b2d92c13f
7
+ data.tar.gz: 85b9bc74278df76106589f3e7bf7185e728cea31bad944e4aba5e838a512a4583583cfc5eec1cfec1f2fbc61b835998cec1ef273868d5bbbf7be87a87d5c825b
@@ -0,0 +1,24 @@
1
+ name: ubuntu
2
+
3
+ on: [push, pull_request]
4
+
5
+ jobs:
6
+ build:
7
+ name: build (${{ matrix.ruby }} / ${{ matrix.os }})
8
+ strategy:
9
+ matrix:
10
+ ruby: [ 2.7, 2.6, 2.5, 2.4, head ]
11
+ os: [ ubuntu-latest, macos-latest ]
12
+ runs-on: ${{ matrix.os }}
13
+ steps:
14
+ - uses: actions/checkout@master
15
+ - name: Set up Ruby
16
+ uses: ruby/setup-ruby@v1
17
+ with:
18
+ ruby-version: ${{ matrix.ruby }}
19
+ - name: Install dependencies
20
+ run: |
21
+ gem install bundler --no-document
22
+ bundle install
23
+ - name: Run test
24
+ run: rake test
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in weakref.gemspec
4
+ gemspec
@@ -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.
@@ -0,0 +1,43 @@
1
+ # Weakref
2
+
3
+ Weak Reference class that allows a referenced object to be
4
+ garbage-collected.
5
+
6
+ A WeakRef may be used exactly like the object it references.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'weakref'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install weakref
23
+
24
+ ## Usage
25
+
26
+ ```ruby
27
+ foo = Object.new # create a new object instance
28
+ p foo.to_s # original's class
29
+ foo = WeakRef.new(foo) # reassign foo with WeakRef instance
30
+ p foo.to_s # should be same class
31
+ GC.start # start the garbage collector
32
+ p foo.to_s # should raise exception (recycled)
33
+ ```
34
+
35
+ ## Development
36
+
37
+ 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.
38
+
39
+ 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).
40
+
41
+ ## Contributing
42
+
43
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ruby/weakref.
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test/lib"
6
+ t.ruby_opts << "-rhelper"
7
+ t.test_files = FileList["test/**/test_*.rb"]
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "weakref"
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__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+ require "delegate"
3
+
4
+ # Weak Reference class that allows a referenced object to be
5
+ # garbage-collected.
6
+ #
7
+ # A WeakRef may be used exactly like the object it references.
8
+ #
9
+ # Usage:
10
+ #
11
+ # foo = Object.new # create a new object instance
12
+ # p foo.to_s # original's class
13
+ # foo = WeakRef.new(foo) # reassign foo with WeakRef instance
14
+ # p foo.to_s # should be same class
15
+ # GC.start # start the garbage collector
16
+ # p foo.to_s # should raise exception (recycled)
17
+ #
18
+
19
+ class WeakRef < Delegator
20
+
21
+ ##
22
+ # RefError is raised when a referenced object has been recycled by the
23
+ # garbage collector
24
+
25
+ class RefError < StandardError
26
+ end
27
+
28
+ @@__map = ::ObjectSpace::WeakMap.new
29
+
30
+ ##
31
+ # Creates a weak reference to +orig+
32
+ #
33
+ # Raises an ArgumentError if the given +orig+ is immutable, such as Symbol,
34
+ # Integer, or Float.
35
+
36
+ def initialize(orig)
37
+ case orig
38
+ when true, false, nil
39
+ @delegate_sd_obj = orig
40
+ else
41
+ @@__map[self] = orig
42
+ end
43
+ super
44
+ end
45
+
46
+ def __getobj__ # :nodoc:
47
+ @@__map[self] or defined?(@delegate_sd_obj) ? @delegate_sd_obj :
48
+ Kernel::raise(RefError, "Invalid Reference - probably recycled", Kernel::caller(2))
49
+ end
50
+
51
+ def __setobj__(obj) # :nodoc:
52
+ end
53
+
54
+ ##
55
+ # Returns true if the referenced object is still alive.
56
+
57
+ def weakref_alive?
58
+ @@__map.key?(self) or defined?(@delegate_sd_obj)
59
+ end
60
+ end
@@ -0,0 +1,3 @@
1
+ module Weakref
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,31 @@
1
+ lib = File.expand_path("lib", __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "weakref/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "weakref"
7
+ spec.version = Weakref::VERSION
8
+ spec.authors = ["Yukihiro Matsumoto"]
9
+ spec.email = ["matz@ruby-lang.org"]
10
+
11
+ spec.summary = %q{Allows a referenced object to be garbage-collected.}
12
+ spec.description = %q{Allows a referenced object to be garbage-collected.}
13
+ spec.homepage = "https://github.com/ruby/weakref"
14
+
15
+ spec.metadata["homepage_uri"] = spec.homepage
16
+ spec.metadata["source_code_uri"] = "https://github.com/ruby/weakref"
17
+ spec.license = "BSD-2-Clause"
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
+ end
24
+ spec.bindir = "exe"
25
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ["lib"]
27
+
28
+ spec.add_development_dependency "bundler", "~> 2.0"
29
+ spec.add_development_dependency "rake", "~> 10.0"
30
+ spec.add_development_dependency "minitest", "~> 5.0"
31
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: weakref
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: 2020-04-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ description: Allows a referenced object to be garbage-collected.
56
+ email:
57
+ - matz@ruby-lang.org
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".github/workflows/test.yml"
63
+ - ".gitignore"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - bin/console
69
+ - bin/setup
70
+ - lib/weakref.rb
71
+ - lib/weakref/version.rb
72
+ - weakref.gemspec
73
+ homepage: https://github.com/ruby/weakref
74
+ licenses:
75
+ - BSD-2-Clause
76
+ metadata:
77
+ homepage_uri: https://github.com/ruby/weakref
78
+ source_code_uri: https://github.com/ruby/weakref
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubygems_version: 3.2.0.pre1
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Allows a referenced object to be garbage-collected.
98
+ test_files: []