fiber-storage 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/lib/fiber/storage/version.rb +1 -1
- data/lib/fiber/storage.rb +40 -5
- data/license.md +1 -1
- data/readme.md +2 -17
- data.tar.gz.sig +0 -0
- metadata +3 -3
- metadata.gz.sig +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 613e39ecf0a56decec406ef7692b26baaf0b273dae3402ef8b84deab22af6015
|
4
|
+
data.tar.gz: dd80f4d293fcc5b3b2359275af6a6834c8b9c474a0befecb77ea164e2e14de9c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 55c768ba68fbc1cd36b09bcf20a9d903fae33d73402d7beb41e98510d76c681f8bee71f9a6b1e1019c6bc5b12486fdca16c7a2eded4725acae052bd0ed6d8be8
|
7
|
+
data.tar.gz: 0e4404d83c7f2a26f492133aa5c15bdecbfbb655ff48cfb081ac56612c3c1f40a87e1df4e41c8fdc1c50be57dd5d68b1be4a72760f88634d8616cad58e9298bb
|
checksums.yaml.gz.sig
CHANGED
Binary file
|
data/lib/fiber/storage.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
# Released under the MIT License.
|
4
|
-
# Copyright, 2022, by Samuel Williams.
|
4
|
+
# Copyright, 2022-2023, by Samuel Williams.
|
5
5
|
|
6
6
|
require 'fiber'
|
7
7
|
|
@@ -11,38 +11,73 @@ class Fiber
|
|
11
11
|
case storage
|
12
12
|
when true
|
13
13
|
@storage = Fiber.current.storage
|
14
|
-
when false
|
15
|
-
@storage = Fiber.current.__storage__
|
16
14
|
else
|
15
|
+
raise TypeError, "Storage must be a hash!" unless storage.is_a?(Hash)
|
16
|
+
|
17
17
|
@storage = storage
|
18
18
|
end
|
19
19
|
|
20
20
|
super(*arguments, **options, &block)
|
21
21
|
end
|
22
22
|
|
23
|
+
# Set the storage associated with this fiber, clearing any previous storage.
|
23
24
|
def storage=(hash)
|
24
25
|
@storage = hash.dup
|
25
26
|
end
|
26
27
|
|
28
|
+
# The storage associated with this fiber.
|
27
29
|
def storage
|
28
30
|
@storage.dup
|
29
31
|
end
|
30
32
|
|
31
33
|
def __storage__
|
32
|
-
@storage
|
34
|
+
@storage ||= {}
|
33
35
|
end
|
34
36
|
end
|
35
37
|
|
36
38
|
unless Fiber.current.respond_to?(:storage)
|
37
|
-
warn "Fiber#storage is
|
39
|
+
warn "Fiber#storage is missing and being monkey-patched."
|
38
40
|
prepend Storage
|
39
41
|
|
42
|
+
# Get a value from the current fiber's storage.
|
40
43
|
def self.[] key
|
44
|
+
raise TypeError, "Key must be symbol!" unless key.is_a?(Symbol)
|
45
|
+
|
41
46
|
self.current.__storage__[key]
|
42
47
|
end
|
43
48
|
|
49
|
+
# Set a value in the current fiber's storage.
|
44
50
|
def self.[]= key, value
|
51
|
+
raise TypeError, "Key must be symbol!" unless key.is_a?(Symbol)
|
52
|
+
|
45
53
|
self.current.__storage__[key] = value
|
46
54
|
end
|
55
|
+
else
|
56
|
+
def self.__borked_keys__
|
57
|
+
!Fiber.new do
|
58
|
+
key = :"#{self.object_id}.key"
|
59
|
+
Fiber[key] = true
|
60
|
+
Fiber[key]
|
61
|
+
end.resume
|
62
|
+
end
|
63
|
+
|
64
|
+
if __borked_keys__
|
65
|
+
module FixBorkedKeys
|
66
|
+
def [](key)
|
67
|
+
raise TypeError, "Key must be symbol!" unless key.is_a?(Symbol)
|
68
|
+
|
69
|
+
super(eval(key.inspect))
|
70
|
+
end
|
71
|
+
|
72
|
+
def []=(key, value)
|
73
|
+
raise TypeError, "Key must be symbol!" unless key.is_a?(Symbol)
|
74
|
+
|
75
|
+
super(eval(key.inspect), value)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
warn "Fiber#storage has borked keys and is being monkey-patched."
|
80
|
+
singleton_class.prepend FixBorkedKeys
|
81
|
+
end
|
47
82
|
end
|
48
83
|
end
|
data/license.md
CHANGED
data/readme.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Fiber::Storage
|
2
2
|
|
3
|
-
This gem provides a shim for `Fiber.[]`, `Fiber.[]=`, `Fiber#storage`, `Fiber#storage
|
3
|
+
This gem provides a shim for `Fiber.[]`, `Fiber.[]=`, `Fiber#storage`, `Fiber#storage=`, which was introduced in Ruby 3.2.
|
4
4
|
|
5
5
|
## Motivation
|
6
6
|
|
@@ -8,21 +8,6 @@ Ruby 3.2 introduces inheritable fiber storage for per-request or per-operation s
|
|
8
8
|
|
9
9
|
Notably, it does not support inheritance across threads or lazy Enumerator. This is a limitation of the shim implementation.
|
10
10
|
|
11
|
-
## Installation
|
12
|
-
|
13
|
-
Add the gem to your project:
|
14
|
-
|
15
|
-
```shell
|
16
|
-
$ bundle add fiber-storage
|
17
|
-
```
|
18
|
-
|
19
11
|
## Usage
|
20
12
|
|
21
|
-
|
22
|
-
require 'fiber/storage'
|
23
|
-
|
24
|
-
Fiber[:x] = 10
|
25
|
-
Fiber.new do
|
26
|
-
Fiber[:x] # => 10
|
27
|
-
end.resume
|
28
|
-
```
|
13
|
+
See the [project documentation](https://ioquatix.github.io/fiber-storage).
|
data.tar.gz.sig
CHANGED
Binary file
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fiber-storage
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Samuel Williams
|
@@ -37,7 +37,7 @@ cert_chain:
|
|
37
37
|
Q2K9NVun/S785AP05vKkXZEFYxqG6EW012U4oLcFl5MySFajYXRYbuUpH6AY+HP8
|
38
38
|
voD0MPg1DssDLKwXyt1eKD/+Fq0bFWhwVM/1XiAXL7lyYUyOq24KHgQ2Csg=
|
39
39
|
-----END CERTIFICATE-----
|
40
|
-
date:
|
40
|
+
date: 2023-08-24 00:00:00.000000000 Z
|
41
41
|
dependencies:
|
42
42
|
- !ruby/object:Gem::Dependency
|
43
43
|
name: bundler
|
@@ -110,7 +110,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
110
110
|
- !ruby/object:Gem::Version
|
111
111
|
version: '0'
|
112
112
|
requirements: []
|
113
|
-
rubygems_version: 3.
|
113
|
+
rubygems_version: 3.4.10
|
114
114
|
signing_key:
|
115
115
|
specification_version: 4
|
116
116
|
summary: Provides a compatibility shim for fiber storage.
|
metadata.gz.sig
CHANGED
Binary file
|