request_store 1.5.0 → 1.5.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +1 -1
- data/README.md +10 -0
- data/lib/request_store/middleware.rb +6 -2
- data/lib/request_store/version.rb +1 -1
- data/request_store.gemspec +1 -1
- data/test/middleware_test.rb +36 -0
- data/test/test_helper.rb +16 -0
- metadata +7 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a8829219a616f7288e08e65fe5d39d491b1693cb31ca5632355a4a4216167a21
|
4
|
+
data.tar.gz: 1f151729fdffa5b98422b359ab3f145caf8a19943bb075f0b5d2b13999b318b3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bc75042c21655c31f3392520e49a7bc816a157a5ac60ba783b73436d5293b88379ac08c0dc94ba352b7f4819c6d48cebccd60315a930a1d600c2a65148f14ae8
|
7
|
+
data.tar.gz: d70cb3dbb0868d4311cdb1ff6b710c9bab96c698acdd0d6d4c409ea133dafe119a43bd9d1eee41eea819350e5187aa4338fb4fda3d69613d6a128f9cbd86c7ea
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -58,6 +58,16 @@ Yep, everywhere you used `Thread.current` just change it to
|
|
58
58
|
`RequestStore.store`. Now no matter what server you use, you'll get `1` every
|
59
59
|
time: the storage is local to that request.
|
60
60
|
|
61
|
+
### API
|
62
|
+
|
63
|
+
The `fetch` method returns the stored value if it already exists. If no stored value exists, it uses the provided block to add a new stored value.
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
top_posts = RequestStore.fetch(:top_posts) do
|
67
|
+
# code to obtain the top posts
|
68
|
+
end
|
69
|
+
```
|
70
|
+
|
61
71
|
### Rails 2 compatibility
|
62
72
|
|
63
73
|
The gem includes a Railtie that will configure everything properly for Rails 3+
|
@@ -16,12 +16,16 @@ module RequestStore
|
|
16
16
|
def call(env)
|
17
17
|
RequestStore.begin!
|
18
18
|
|
19
|
-
|
19
|
+
status, headers, body = @app.call(env)
|
20
20
|
|
21
|
-
|
21
|
+
body = Rack::BodyProxy.new(body) do
|
22
22
|
RequestStore.end!
|
23
23
|
RequestStore.clear!
|
24
24
|
end
|
25
|
+
|
26
|
+
returned = true
|
27
|
+
|
28
|
+
[status, headers, body]
|
25
29
|
ensure
|
26
30
|
unless returned
|
27
31
|
RequestStore.end!
|
data/request_store.gemspec
CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |gem|
|
|
10
10
|
gem.email = ["steve@steveklabnik.com"]
|
11
11
|
gem.description = %q{RequestStore gives you per-request global storage.}
|
12
12
|
gem.summary = %q{RequestStore gives you per-request global storage.}
|
13
|
-
gem.homepage = "
|
13
|
+
gem.homepage = "https://github.com/steveklabnik/request_store"
|
14
14
|
gem.licenses = ["MIT"]
|
15
15
|
|
16
16
|
gem.files = `git ls-files`.split($/)
|
data/test/middleware_test.rb
CHANGED
@@ -12,6 +12,7 @@ class MiddlewareTest < Minitest::Test
|
|
12
12
|
def call_middleware(opts = {})
|
13
13
|
_, _, proxy = @middleware.call(opts)
|
14
14
|
proxy.close
|
15
|
+
proxy
|
15
16
|
end
|
16
17
|
|
17
18
|
def test_middleware_resets_store
|
@@ -23,6 +24,17 @@ class MiddlewareTest < Minitest::Test
|
|
23
24
|
assert_equal({}, RequestStore.store)
|
24
25
|
end
|
25
26
|
|
27
|
+
def test_middleware_does_not_mutate_response_and_does_not_overflow_stack
|
28
|
+
10000.times do
|
29
|
+
call_middleware
|
30
|
+
end
|
31
|
+
|
32
|
+
resp = call_middleware
|
33
|
+
assert resp.is_a?(::Rack::BodyProxy)
|
34
|
+
assert_equal ["response"], resp.to_a
|
35
|
+
assert_equal ["response"], resp.instance_variable_get(:@body)
|
36
|
+
end
|
37
|
+
|
26
38
|
def test_middleware_resets_store_on_error
|
27
39
|
e = assert_raises RuntimeError do
|
28
40
|
call_middleware({:error => true})
|
@@ -63,3 +75,27 @@ class MiddlewareTest < Minitest::Test
|
|
63
75
|
refute RequestStore.store[:foo]
|
64
76
|
end
|
65
77
|
end
|
78
|
+
|
79
|
+
class MiddlewareWithConstResponseTest < Minitest::Test
|
80
|
+
def setup
|
81
|
+
@app = RackAppWithConstResponse.new
|
82
|
+
@middleware = RequestStore::Middleware.new(@app)
|
83
|
+
end
|
84
|
+
|
85
|
+
def call_middleware(opts = {})
|
86
|
+
_, _, proxy = @middleware.call(opts)
|
87
|
+
proxy.close
|
88
|
+
proxy
|
89
|
+
end
|
90
|
+
|
91
|
+
def test_middleware_does_not_mutate_response_and_does_not_overflow_stack
|
92
|
+
10000.times do
|
93
|
+
call_middleware
|
94
|
+
end
|
95
|
+
|
96
|
+
resp = call_middleware
|
97
|
+
assert resp.is_a?(::Rack::BodyProxy)
|
98
|
+
assert_equal ["response"], resp.to_a
|
99
|
+
assert_equal ["response"], resp.instance_variable_get(:@body)
|
100
|
+
end
|
101
|
+
end
|
data/test/test_helper.rb
CHANGED
@@ -11,3 +11,19 @@ class RackApp
|
|
11
11
|
[200, {}, ["response"]]
|
12
12
|
end
|
13
13
|
end
|
14
|
+
|
15
|
+
class RackAppWithConstResponse
|
16
|
+
RESPONSE = [200, {}, ["response"]]
|
17
|
+
|
18
|
+
attr_reader :last_value, :store_active
|
19
|
+
|
20
|
+
def call(env)
|
21
|
+
RequestStore.store[:foo] ||= 0
|
22
|
+
RequestStore.store[:foo] += 1
|
23
|
+
@last_value = RequestStore.store[:foo]
|
24
|
+
@store_active = RequestStore.active?
|
25
|
+
raise 'FAIL' if env[:error]
|
26
|
+
|
27
|
+
RESPONSE
|
28
|
+
end
|
29
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: request_store
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.5.
|
4
|
+
version: 1.5.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Steve Klabnik
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-01-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rack
|
@@ -73,11 +73,11 @@ files:
|
|
73
73
|
- test/middleware_test.rb
|
74
74
|
- test/request_store_test.rb
|
75
75
|
- test/test_helper.rb
|
76
|
-
homepage:
|
76
|
+
homepage: https://github.com/steveklabnik/request_store
|
77
77
|
licenses:
|
78
78
|
- MIT
|
79
79
|
metadata: {}
|
80
|
-
post_install_message:
|
80
|
+
post_install_message:
|
81
81
|
rdoc_options: []
|
82
82
|
require_paths:
|
83
83
|
- lib
|
@@ -92,8 +92,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
92
92
|
- !ruby/object:Gem::Version
|
93
93
|
version: '0'
|
94
94
|
requirements: []
|
95
|
-
rubygems_version: 3.
|
96
|
-
signing_key:
|
95
|
+
rubygems_version: 3.2.15
|
96
|
+
signing_key:
|
97
97
|
specification_version: 4
|
98
98
|
summary: RequestStore gives you per-request global storage.
|
99
99
|
test_files:
|