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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 77ad72476d9a5209661f380069bcfc6d4478cc7e3a7e7c7eadea738051bdbf9d
4
- data.tar.gz: 0bf1132f9b6902487c93fd66c9bec0ec2c50485a15a5f7fe5e2b8907142e1a25
3
+ metadata.gz: 613e39ecf0a56decec406ef7692b26baaf0b273dae3402ef8b84deab22af6015
4
+ data.tar.gz: dd80f4d293fcc5b3b2359275af6a6834c8b9c474a0befecb77ea164e2e14de9c
5
5
  SHA512:
6
- metadata.gz: 17578725c8cd96ba921ee24273f9a9134a391102b9f1cecbba8ce66983d93663d00a3bffe0565c72b365767d43fac31a329c166ade9a5c14461c1491e19759d1
7
- data.tar.gz: b9ea2b14c0bc99ced0b169f659f8b110154819ad83656a4015d316aa62c93c1af006eed54944c7ab95e3fedc8abb24e5ce975b00288b75f430e5f5cfde0a526e
6
+ metadata.gz: 55c768ba68fbc1cd36b09bcf20a9d903fae33d73402d7beb41e98510d76c681f8bee71f9a6b1e1019c6bc5b12486fdca16c7a2eded4725acae052bd0ed6d8be8
7
+ data.tar.gz: 0e4404d83c7f2a26f492133aa5c15bdecbfbb655ff48cfb081ac56612c3c1f40a87e1df4e41c8fdc1c50be57dd5d68b1be4a72760f88634d8616cad58e9298bb
checksums.yaml.gz.sig CHANGED
Binary file
@@ -7,6 +7,6 @@ require 'fiber'
7
7
 
8
8
  class Fiber
9
9
  module Storage
10
- VERSION = "0.0.1"
10
+ VERSION = "0.1.0"
11
11
  end
12
12
  end
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 running in compatibility mode."
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
@@ -1,6 +1,6 @@
1
1
  # MIT License
2
2
 
3
- Copyright, 2022, by Samuel Williams.
3
+ Copyright, 2022-2023, by Samuel Williams.
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
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
- ```ruby
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.1
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: 2022-12-02 00:00:00.000000000 Z
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.3.7
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