request_store 1.3.2 → 1.5.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4940fb899c4f08b98633877ca272f29dcdd4ade30456bc87a2529d38da9385e8
4
+ data.tar.gz: 8be86e88c85f7aeaa3b0ac1eee98da6b26f9c95595319fc010eb800e542eb3cf
5
+ SHA512:
6
+ metadata.gz: 59f193bbe4aaa3e1a13d5540840b379eadfd654f3f30ac5c9f8dcd780357f0fcc88a2a70d85f3637934a9eff9297cf4d410ceb2829a8ccdc2f6c9cf0631d1579
7
+ data.tar.gz: 1727397fd735206c9260e5957a78ec9014966619024e7083d29e5ccab5b71178794a2aa1892f0801bbd4afd267a2b7a3b75728a0fc086d0575be2ac477040501
data/.gitignore CHANGED
@@ -1,17 +1,17 @@
1
- *.gem
2
- *.rbc
3
- .bundle
4
- .config
5
- .yardoc
6
- Gemfile.lock
7
- InstalledFiles
8
- _yardoc
9
- coverage
10
- doc/
11
- lib/bundler/man
12
- pkg
13
- rdoc
14
- spec/reports
15
- test/tmp
16
- test/version_tmp
17
- tmp
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.travis.yml CHANGED
@@ -1,19 +1,18 @@
1
- language: ruby
2
- sudo: false
3
- rvm:
4
- - 1.8.7
5
- - 1.9.2
6
- - 1.9.3
7
- - 2.0.0
8
- - 2.1.0
9
- - 2.2.3
10
- - jruby-18mode
11
- - jruby-19mode
12
- - rbx-2
13
- - ruby-head
14
- - jruby-head
15
- - ree
16
- matrix:
17
- allow_failures:
18
- - rvm: ruby-head
19
- - rvm: jruby-head
1
+ language: ruby
2
+ sudo: false
3
+ rvm:
4
+ - 1.9.3
5
+ - 2.0.0
6
+ - 2.1.0
7
+ - 2.2.3
8
+ - 2.4.9
9
+ - 2.5.7
10
+ - 2.6.5
11
+ - jruby-18mode
12
+ - jruby-19mode
13
+ - ruby-head
14
+ - jruby-head
15
+ matrix:
16
+ allow_failures:
17
+ - rvm: ruby-head
18
+ - rvm: jruby-head
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
- source 'https://rubygems.org'
2
-
3
- # Specify your gem's dependencies in request_store.gemspec
4
- gemspec
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in request_store.gemspec
4
+ gemspec
data/LICENSE.txt CHANGED
@@ -1,22 +1,22 @@
1
- Copyright (c) 2012 Steve Klabnik
2
-
3
- MIT License
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining
6
- a copy of this software and associated documentation files (the
7
- "Software"), to deal in the Software without restriction, including
8
- without limitation the rights to use, copy, modify, merge, publish,
9
- distribute, sublicense, and/or sell copies of the Software, and to
10
- permit persons to whom the Software is furnished to do so, subject to
11
- the following conditions:
12
-
13
- The above copyright notice and this permission notice shall be
14
- included in all copies or substantial portions of the Software.
15
-
16
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
1
+ Copyright (c) 2012 Steve Klabnik
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
22
  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -1,126 +1,137 @@
1
- # RequestStore [![build status](https://travis-ci.org/steveklabnik/request_store.png?branch=master)](https://travis-ci.org/steveklabnik/request_store) [![Code Climate](https://codeclimate.com/github/steveklabnik/request_store.png)](https://codeclimate.com/github/steveklabnik/request_store)
2
-
3
- Ever needed to use a global variable in Rails? Ugh, that's the worst. If you
4
- need global state, you've probably reached for `Thread.current`. Like this:
5
-
6
- ```ruby
7
- def self.foo
8
- Thread.current[:foo] ||= 0
9
- end
10
-
11
- def self.foo=(value)
12
- Thread.current[:foo] = value
13
- end
14
- ```
15
-
16
- Ugh! I hate it. But you gotta do what you gotta do...
17
-
18
- ### The problem
19
-
20
- Everyone's worrying about concurrency these days. So people are using those
21
- fancy threaded web servers, like Thin or Puma. But if you use `Thread.current`,
22
- and you use one of those servers, watch out! Values can stick around longer
23
- than you'd expect, and this can cause bugs. For example, if we had this in
24
- our controller:
25
-
26
- ```ruby
27
- def index
28
- Thread.current[:counter] ||= 0
29
- Thread.current[:counter] += 1
30
-
31
- render :text => Thread.current[:counter]
32
- end
33
- ```
34
-
35
- If we ran this on MRI with Webrick, you'd get `1` as output, every time. But if
36
- you run it with Thin, you get `1`, then `2`, then `3`...
37
-
38
- ### The solution
39
-
40
- Add this line to your application's Gemfile:
41
-
42
- ```ruby
43
- gem 'request_store'
44
- ```
45
-
46
- And change the code to this:
47
-
48
- ```ruby
49
- def index
50
- RequestStore.store[:foo] ||= 0
51
- RequestStore.store[:foo] += 1
52
-
53
- render :text => RequestStore.store[:foo]
54
- end
55
- ```
56
-
57
- Yep, everywhere you used `Thread.current` just change it to
58
- `RequestStore.store`. Now no matter what server you use, you'll get `1` every
59
- time: the storage is local to that request.
60
-
61
- ### Rails 2 compatibility
62
-
63
- The gem includes a Railtie that will configure everything properly for Rails 3+
64
- apps, but if your app is tied to an older (2.x) version, you will have to
65
- manually add the middleware yourself. Typically this should just be a matter
66
- of adding:
67
-
68
- ```ruby
69
- config.middleware.use RequestStore::Middleware
70
- ```
71
-
72
- into your config/environment.rb.
73
-
74
- ### No Rails? No Problem!
75
-
76
- A Railtie is added that configures the Middleware for you, but if you're not
77
- using Rails, no biggie! Just use the Middleware yourself, however you need.
78
- You'll probably have to shove this somewhere:
79
-
80
- ```ruby
81
- use RequestStore::Middleware
82
- ```
83
-
84
- #### No Rails + Rack::Test
85
-
86
- In order to have `RequestStore` storage cleared between requests, add it to the
87
- `app`:
88
-
89
- ```ruby
90
- # spec_helper.rb
91
-
92
- def app
93
- Rack::Builder.new do
94
- use RequestStore::Middleware
95
- run MyApp
96
- end
97
- end
98
- ```
99
-
100
- ## Semantic Versioning
101
-
102
- This project conforms to [semver](http://semver.org/). As a result of this
103
- policy, you can (and should) specify a dependency on this gem using the
104
- [Pessimistic Version Constraint](http://guides.rubygems.org/patterns/) with
105
- two digits of precision. For example:
106
-
107
- ```ruby
108
- spec.add_dependency 'request_store', '~> 1.0'
109
- ```
110
-
111
- This means your project is compatible with request_store 1.0 up until 2.0.
112
- You can also set a higher minimum version:
113
-
114
- ```ruby
115
- spec.add_dependency 'request_store', '~> 1.1'
116
- ```
117
-
118
- ## Contributing
119
-
120
- 1. Fork it
121
- 2. Create your feature branch (`git checkout -b my-new-feature`)
122
- 3. Commit your changes (`git commit -am 'Add some feature'`)
123
- 4. Push to the branch (`git push origin my-new-feature`)
124
- 5. Create new Pull Request
125
-
126
- Don't forget to run the tests with `rake`.
1
+ # RequestStore [![build status](https://travis-ci.org/steveklabnik/request_store.svg?branch=master)](https://travis-ci.org/steveklabnik/request_store) [![Code Climate](https://codeclimate.com/github/steveklabnik/request_store.svg)](https://codeclimate.com/github/steveklabnik/request_store)
2
+
3
+ Ever needed to use a global variable in Rails? Ugh, that's the worst. If you
4
+ need global state, you've probably reached for `Thread.current`. Like this:
5
+
6
+ ```ruby
7
+ def self.foo
8
+ Thread.current[:foo] ||= 0
9
+ end
10
+
11
+ def self.foo=(value)
12
+ Thread.current[:foo] = value
13
+ end
14
+ ```
15
+
16
+ Ugh! I hate it. But you gotta do what you gotta do...
17
+
18
+ ### The problem
19
+
20
+ Everyone's worrying about concurrency these days. So people are using those
21
+ fancy threaded web servers, like Thin or Puma. But if you use `Thread.current`,
22
+ and you use one of those servers, watch out! Values can stick around longer
23
+ than you'd expect, and this can cause bugs. For example, if we had this in
24
+ our controller:
25
+
26
+ ```ruby
27
+ def index
28
+ Thread.current[:counter] ||= 0
29
+ Thread.current[:counter] += 1
30
+
31
+ render :text => Thread.current[:counter]
32
+ end
33
+ ```
34
+
35
+ If we ran this on MRI with Webrick, you'd get `1` as output, every time. But if
36
+ you run it with Thin, you get `1`, then `2`, then `3`...
37
+
38
+ ### The solution
39
+
40
+ Add this line to your application's Gemfile:
41
+
42
+ ```ruby
43
+ gem 'request_store'
44
+ ```
45
+
46
+ And change the code to this:
47
+
48
+ ```ruby
49
+ def index
50
+ RequestStore.store[:foo] ||= 0
51
+ RequestStore.store[:foo] += 1
52
+
53
+ render :text => RequestStore.store[:foo]
54
+ end
55
+ ```
56
+
57
+ Yep, everywhere you used `Thread.current` just change it to
58
+ `RequestStore.store`. Now no matter what server you use, you'll get `1` every
59
+ time: the storage is local to that request.
60
+
61
+ ### Rails 2 compatibility
62
+
63
+ The gem includes a Railtie that will configure everything properly for Rails 3+
64
+ apps, but if your app is tied to an older (2.x) version, you will have to
65
+ manually add the middleware yourself. Typically this should just be a matter
66
+ of adding:
67
+
68
+ ```ruby
69
+ config.middleware.use RequestStore::Middleware
70
+ ```
71
+
72
+ into your config/environment.rb.
73
+
74
+ ### No Rails? No Problem!
75
+
76
+ A Railtie is added that configures the Middleware for you, but if you're not
77
+ using Rails, no biggie! Just use the Middleware yourself, however you need.
78
+ You'll probably have to shove this somewhere:
79
+
80
+ ```ruby
81
+ use RequestStore::Middleware
82
+ ```
83
+
84
+ #### No Rails + Rack::Test
85
+
86
+ In order to have `RequestStore` storage cleared between requests, add it to the
87
+ `app`:
88
+
89
+ ```ruby
90
+ # spec_helper.rb
91
+
92
+ def app
93
+ Rack::Builder.new do
94
+ use RequestStore::Middleware
95
+ run MyApp
96
+ end
97
+ end
98
+ ```
99
+
100
+ ## Using with Sidekiq
101
+
102
+ This gem uses a Rack middleware to clear the store object after every request,
103
+ but that doesn't translate well to background processing with
104
+ [Sidekiq](https://github.com/mperham/sidekiq).
105
+
106
+ A companion library,
107
+ [request_store-sidekiq](https://rubygems.org/gems/request_store-sidekiq)
108
+ creates a Sidekiq middleware that will ensure the store is cleared after each
109
+ job is processed, for security and consistency with how this is done in Rack.
110
+
111
+ ## Semantic Versioning
112
+
113
+ This project conforms to [semver](http://semver.org/). As a result of this
114
+ policy, you can (and should) specify a dependency on this gem using the
115
+ [Pessimistic Version Constraint](http://guides.rubygems.org/patterns/) with
116
+ two digits of precision. For example:
117
+
118
+ ```ruby
119
+ spec.add_dependency 'request_store', '~> 1.0'
120
+ ```
121
+
122
+ This means your project is compatible with request_store 1.0 up until 2.0.
123
+ You can also set a higher minimum version:
124
+
125
+ ```ruby
126
+ spec.add_dependency 'request_store', '~> 1.1'
127
+ ```
128
+
129
+ ## Contributing
130
+
131
+ 1. Fork it
132
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
133
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
134
+ 4. Push to the branch (`git push origin my-new-feature`)
135
+ 5. Create new Pull Request
136
+
137
+ Don't forget to run the tests with `rake`.
data/Rakefile CHANGED
@@ -1,12 +1,12 @@
1
- require "bundler/gem_tasks"
2
-
3
- require 'rake/testtask'
4
-
5
- Rake::TestTask.new do |t|
6
- t.libs << "lib"
7
- t.test_files = FileList['test/*_test.rb']
8
- t.ruby_opts = ['-r./test/test_helper.rb']
9
- t.verbose = true
10
- end
11
-
12
- task :default => :test
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rake/testtask'
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/*_test.rb']
8
+ t.ruby_opts = ['-r./test/test_helper.rb']
9
+ t.verbose = true
10
+ end
11
+
12
+ task :default => :test
@@ -1,15 +1,32 @@
1
- module RequestStore
2
- class Middleware
3
- def initialize(app)
4
- @app = app
5
- end
6
-
7
- def call(env)
8
- RequestStore.begin!
9
- @app.call(env)
10
- ensure
11
- RequestStore.end!
12
- RequestStore.clear!
13
- end
14
- end
15
- end
1
+ require 'rack/body_proxy'
2
+
3
+ # A middleware that ensures the RequestStore stays around until
4
+ # the last part of the body is rendered. This is useful when
5
+ # using streaming.
6
+ #
7
+ # Uses Rack::BodyProxy, adapted from Rack::Lock's usage of the
8
+ # same pattern.
9
+
10
+ module RequestStore
11
+ class Middleware
12
+ def initialize(app)
13
+ @app = app
14
+ end
15
+
16
+ def call(env)
17
+ RequestStore.begin!
18
+
19
+ response = @app.call(env)
20
+
21
+ returned = response << Rack::BodyProxy.new(response.pop) do
22
+ RequestStore.end!
23
+ RequestStore.clear!
24
+ end
25
+ ensure
26
+ unless returned
27
+ RequestStore.end!
28
+ RequestStore.clear!
29
+ end
30
+ end
31
+ end
32
+ end
@@ -1,21 +1,21 @@
1
- module RequestStore
2
- class Railtie < ::Rails::Railtie
3
- initializer "request_store.insert_middleware" do |app|
4
- if ActionDispatch.const_defined? :RequestId
5
- app.config.middleware.insert_after ActionDispatch::RequestId, RequestStore::Middleware
6
- else
7
- app.config.middleware.insert_after Rack::MethodOverride, RequestStore::Middleware
8
- end
9
-
10
- if ActiveSupport.const_defined?(:Reloader) && ActiveSupport::Reloader.respond_to?(:to_complete)
11
- ActiveSupport::Reloader.to_complete do
12
- RequestStore.clear!
13
- end
14
- elsif ActionDispatch.const_defined?(:Reloader) && ActionDispatch::Reloader.respond_to?(:to_cleanup)
15
- ActionDispatch::Reloader.to_cleanup do
16
- RequestStore.clear!
17
- end
18
- end
19
- end
20
- end
21
- end
1
+ module RequestStore
2
+ class Railtie < ::Rails::Railtie
3
+ initializer "request_store.insert_middleware" do |app|
4
+ if ActionDispatch.const_defined? :RequestId
5
+ app.config.middleware.insert_after ActionDispatch::RequestId, RequestStore::Middleware
6
+ else
7
+ app.config.middleware.insert_after Rack::MethodOverride, RequestStore::Middleware
8
+ end
9
+
10
+ if ActiveSupport.const_defined?(:Reloader) && ActiveSupport::Reloader.respond_to?(:to_complete)
11
+ ActiveSupport::Reloader.to_complete do
12
+ RequestStore.clear!
13
+ end
14
+ elsif ActionDispatch.const_defined?(:Reloader) && ActionDispatch::Reloader.respond_to?(:to_cleanup)
15
+ ActionDispatch::Reloader.to_cleanup do
16
+ RequestStore.clear!
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -1,3 +1,3 @@
1
- module RequestStore
2
- VERSION = "1.3.2"
3
- end
1
+ module RequestStore
2
+ VERSION = "1.5.0"
3
+ end
data/lib/request_store.rb CHANGED
@@ -1,54 +1,58 @@
1
- require "request_store/version"
2
- require "request_store/middleware"
3
- require "request_store/railtie" if defined?(Rails::Railtie)
4
-
5
- module RequestStore
6
- def self.store
7
- Thread.current[:request_store] ||= {}
8
- end
9
-
10
- def self.clear!
11
- Thread.current[:request_store] = {}
12
- end
13
-
14
- def self.begin!
15
- Thread.current[:request_store_active] = true
16
- end
17
-
18
- def self.end!
19
- Thread.current[:request_store_active] = false
20
- end
21
-
22
- def self.active?
23
- Thread.current[:request_store_active] || false
24
- end
25
-
26
- def self.read(key)
27
- store[key]
28
- end
29
-
30
- def self.[](key)
31
- store[key]
32
- end
33
-
34
- def self.write(key, value)
35
- store[key] = value
36
- end
37
-
38
- def self.[]=(key, value)
39
- store[key] = value
40
- end
41
-
42
- def self.exist?(key)
43
- store.key?(key)
44
- end
45
-
46
- def self.fetch(key, &block)
47
- store[key] = yield unless exist?(key)
48
- store[key]
49
- end
50
-
51
- def self.delete(key, &block)
52
- store.delete(key, &block)
53
- end
54
- end
1
+ require "request_store/version"
2
+ require "request_store/middleware"
3
+ require "request_store/railtie" if defined?(Rails::Railtie)
4
+
5
+ module RequestStore
6
+ def self.store
7
+ Thread.current[:request_store] ||= {}
8
+ end
9
+
10
+ def self.store=(store)
11
+ Thread.current[:request_store] = store
12
+ end
13
+
14
+ def self.clear!
15
+ Thread.current[:request_store] = {}
16
+ end
17
+
18
+ def self.begin!
19
+ Thread.current[:request_store_active] = true
20
+ end
21
+
22
+ def self.end!
23
+ Thread.current[:request_store_active] = false
24
+ end
25
+
26
+ def self.active?
27
+ Thread.current[:request_store_active] || false
28
+ end
29
+
30
+ def self.read(key)
31
+ store[key]
32
+ end
33
+
34
+ def self.[](key)
35
+ store[key]
36
+ end
37
+
38
+ def self.write(key, value)
39
+ store[key] = value
40
+ end
41
+
42
+ def self.[]=(key, value)
43
+ store[key] = value
44
+ end
45
+
46
+ def self.exist?(key)
47
+ store.key?(key)
48
+ end
49
+
50
+ def self.fetch(key)
51
+ store[key] = yield unless exist?(key)
52
+ store[key]
53
+ end
54
+
55
+ def self.delete(key, &block)
56
+ store.delete(key, &block)
57
+ end
58
+ end
@@ -1,23 +1,25 @@
1
- # -*- encoding: utf-8 -*-
2
- lib = File.expand_path('../lib', __FILE__)
3
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'request_store/version'
5
-
6
- Gem::Specification.new do |gem|
7
- gem.name = "request_store"
8
- gem.version = RequestStore::VERSION
9
- gem.authors = ["Steve Klabnik"]
10
- gem.email = ["steve@steveklabnik.com"]
11
- gem.description = %q{RequestStore gives you per-request global storage.}
12
- gem.summary = %q{RequestStore gives you per-request global storage.}
13
- gem.homepage = "http://github.com/steveklabnik/request_store"
14
- gem.licenses = ["MIT"]
15
-
16
- gem.files = `git ls-files`.split($/)
17
- gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
18
- gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
- gem.require_paths = ["lib"]
20
-
21
- gem.add_development_dependency "rake", "~> 10.5"
22
- gem.add_development_dependency "minitest", "~> 5.0"
23
- end
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'request_store/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "request_store"
8
+ gem.version = RequestStore::VERSION
9
+ gem.authors = ["Steve Klabnik"]
10
+ gem.email = ["steve@steveklabnik.com"]
11
+ gem.description = %q{RequestStore gives you per-request global storage.}
12
+ gem.summary = %q{RequestStore gives you per-request global storage.}
13
+ gem.homepage = "http://github.com/steveklabnik/request_store"
14
+ gem.licenses = ["MIT"]
15
+
16
+ gem.files = `git ls-files`.split($/)
17
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
18
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
+ gem.require_paths = ["lib"]
20
+
21
+ gem.add_dependency "rack", ">= 1.4"
22
+
23
+ gem.add_development_dependency "rake", "~> 10.5"
24
+ gem.add_development_dependency "minitest", "~> 5.0"
25
+ end
@@ -1,45 +1,65 @@
1
- require 'minitest/test'
2
- require 'minitest/autorun'
3
-
4
- require 'request_store'
5
-
6
- class MiddlewareTest < Minitest::Test
7
- def setup
8
- @app = RackApp.new
9
- @middleware = RequestStore::Middleware.new(@app)
10
- end
11
-
12
- def test_middleware_resets_store
13
- 2.times { @middleware.call({}) }
14
-
15
- assert_equal 1, @app.last_value
16
- assert_equal({}, RequestStore.store)
17
- end
18
-
19
- def test_middleware_resets_store_on_error
20
- e = assert_raises RuntimeError do
21
- @middleware.call({:error => true})
22
- end
23
-
24
- assert_equal 'FAIL', e.message
25
- assert_equal({}, RequestStore.store)
26
- end
27
-
28
- def test_middleware_begins_store
29
- @middleware.call({})
30
- assert_equal true, @app.store_active
31
- end
32
-
33
- def test_middleware_ends_store
34
- @middleware.call({})
35
- assert_equal false, RequestStore.active?
36
- end
37
-
38
- def test_middleware_ends_store_on_error
39
- assert_raises RuntimeError do
40
- @middleware.call({:error => true})
41
- end
42
-
43
- assert_equal false, RequestStore.active?
44
- end
45
- end
1
+ require 'minitest/test'
2
+ require 'minitest/autorun'
3
+
4
+ require 'request_store'
5
+
6
+ class MiddlewareTest < Minitest::Test
7
+ def setup
8
+ @app = RackApp.new
9
+ @middleware = RequestStore::Middleware.new(@app)
10
+ end
11
+
12
+ def call_middleware(opts = {})
13
+ _, _, proxy = @middleware.call(opts)
14
+ proxy.close
15
+ end
16
+
17
+ def test_middleware_resets_store
18
+ 2.times do
19
+ call_middleware
20
+ end
21
+
22
+ assert_equal 1, @app.last_value
23
+ assert_equal({}, RequestStore.store)
24
+ end
25
+
26
+ def test_middleware_resets_store_on_error
27
+ e = assert_raises RuntimeError do
28
+ call_middleware({:error => true})
29
+ end
30
+
31
+ assert_equal 'FAIL', e.message
32
+ assert_equal({}, RequestStore.store)
33
+ end
34
+
35
+ def test_middleware_begins_store
36
+ call_middleware
37
+ assert_equal true, @app.store_active
38
+ end
39
+
40
+ def test_middleware_ends_store
41
+ call_middleware
42
+
43
+ assert_equal false, RequestStore.active?
44
+ end
45
+
46
+ def test_middleware_ends_store_on_error
47
+ assert_raises RuntimeError do
48
+ call_middleware({:error => true})
49
+ end
50
+
51
+ assert_equal false, RequestStore.active?
52
+ end
53
+
54
+ def test_middleware_stores_until_proxy_closes
55
+ _, _, proxy = @middleware.call({})
56
+
57
+ assert_equal 1, @app.last_value
58
+ assert RequestStore.active?
59
+
60
+ proxy.close
61
+
62
+ refute RequestStore.active?
63
+ refute RequestStore.store[:foo]
64
+ end
65
+ end
@@ -1,69 +1,80 @@
1
- require 'minitest/autorun'
2
-
3
- require 'request_store'
4
-
5
- class RequestStoreTest < Minitest::Test
6
- def setup
7
- RequestStore.clear!
8
- end
9
-
10
- def test_initial_state
11
- Thread.current[:request_store] = nil
12
- assert_equal RequestStore.store, Hash.new
13
- end
14
-
15
- def test_init_with_hash
16
- assert_equal Hash.new, RequestStore.store
17
- end
18
-
19
- def test_clear
20
- RequestStore.store[:foo] = 1
21
- RequestStore.clear!
22
- assert_equal Hash.new, RequestStore.store
23
- end
24
-
25
- def test_quacks_like_hash
26
- RequestStore.store[:foo] = 1
27
- assert_equal 1, RequestStore.store[:foo]
28
- assert_equal 1, RequestStore.store.fetch(:foo)
29
- end
30
-
31
- def test_read
32
- RequestStore.store[:foo] = 1
33
- assert_equal 1, RequestStore.read(:foo)
34
- assert_equal 1, RequestStore[:foo]
35
- end
36
-
37
- def test_write
38
- RequestStore.write(:foo, 1)
39
- assert_equal 1, RequestStore.store[:foo]
40
- RequestStore[:foo] = 2
41
- assert_equal 2, RequestStore.store[:foo]
42
- end
43
-
44
- def test_fetch
45
- assert_equal 2, RequestStore.fetch(:foo) { 1 + 1 }
46
- assert_equal 2, RequestStore.fetch(:foo) { 2 + 2 }
47
- end
48
-
49
- def test_delete
50
- assert_equal 2, RequestStore.fetch(:foo) { 1 + 1 }
51
- assert_equal 2, RequestStore.delete(:foo) { 2 + 2 }
52
- assert_equal 4, RequestStore.delete(:foo) { 2 + 2 }
53
- end
54
-
55
- def test_delegates_to_thread
56
- RequestStore.store[:foo] = 1
57
- assert_equal 1, Thread.current[:request_store][:foo]
58
- end
59
-
60
- def test_active_state
61
- assert_equal false, RequestStore.active?
62
-
63
- RequestStore.begin!
64
- assert_equal true, RequestStore.active?
65
-
66
- RequestStore.end!
67
- assert_equal false, RequestStore.active?
68
- end
69
- end
1
+ require 'minitest/autorun'
2
+
3
+ require 'request_store'
4
+
5
+ class RequestStoreTest < Minitest::Test
6
+ def setup
7
+ RequestStore.clear!
8
+ end
9
+
10
+ def teardown
11
+ RequestStore.clear!
12
+ end
13
+
14
+ def test_initial_state
15
+ Thread.current[:request_store] = nil
16
+ assert_equal RequestStore.store, Hash.new
17
+ end
18
+
19
+ def test_init_with_hash
20
+ assert_equal Hash.new, RequestStore.store
21
+ end
22
+
23
+ def test_assign_store
24
+ store_obj = { test_key: 'test' }
25
+ RequestStore.store = store_obj
26
+ assert_equal 'test', RequestStore.store[:test_key]
27
+ assert_equal store_obj, RequestStore.store
28
+ end
29
+
30
+ def test_clear
31
+ RequestStore.store[:foo] = 1
32
+ RequestStore.clear!
33
+ assert_equal Hash.new, RequestStore.store
34
+ end
35
+
36
+ def test_quacks_like_hash
37
+ RequestStore.store[:foo] = 1
38
+ assert_equal 1, RequestStore.store[:foo]
39
+ assert_equal 1, RequestStore.store.fetch(:foo)
40
+ end
41
+
42
+ def test_read
43
+ RequestStore.store[:foo] = 1
44
+ assert_equal 1, RequestStore.read(:foo)
45
+ assert_equal 1, RequestStore[:foo]
46
+ end
47
+
48
+ def test_write
49
+ RequestStore.write(:foo, 1)
50
+ assert_equal 1, RequestStore.store[:foo]
51
+ RequestStore[:foo] = 2
52
+ assert_equal 2, RequestStore.store[:foo]
53
+ end
54
+
55
+ def test_fetch
56
+ assert_equal 2, RequestStore.fetch(:foo) { 1 + 1 }
57
+ assert_equal 2, RequestStore.fetch(:foo) { 2 + 2 }
58
+ end
59
+
60
+ def test_delete
61
+ assert_equal 2, RequestStore.fetch(:foo) { 1 + 1 }
62
+ assert_equal 2, RequestStore.delete(:foo) { 2 + 2 }
63
+ assert_equal 4, RequestStore.delete(:foo) { 2 + 2 }
64
+ end
65
+
66
+ def test_delegates_to_thread
67
+ RequestStore.store[:foo] = 1
68
+ assert_equal 1, Thread.current[:request_store][:foo]
69
+ end
70
+
71
+ def test_active_state
72
+ assert_equal false, RequestStore.active?
73
+
74
+ RequestStore.begin!
75
+ assert_equal true, RequestStore.active?
76
+
77
+ RequestStore.end!
78
+ assert_equal false, RequestStore.active?
79
+ end
80
+ end
data/test/test_helper.rb CHANGED
@@ -1,11 +1,13 @@
1
- class RackApp
2
- attr_reader :last_value, :store_active
3
-
4
- def call(env)
5
- RequestStore.store[:foo] ||= 0
6
- RequestStore.store[:foo] += 1
7
- @last_value = RequestStore.store[:foo]
8
- @store_active = RequestStore.active?
9
- raise 'FAIL' if env[:error]
10
- end
11
- end
1
+ class RackApp
2
+ attr_reader :last_value, :store_active
3
+
4
+ def call(env)
5
+ RequestStore.store[:foo] ||= 0
6
+ RequestStore.store[:foo] += 1
7
+ @last_value = RequestStore.store[:foo]
8
+ @store_active = RequestStore.active?
9
+ raise 'FAIL' if env[:error]
10
+
11
+ [200, {}, ["response"]]
12
+ end
13
+ end
metadata CHANGED
@@ -1,46 +1,55 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: request_store
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.2
5
- prerelease:
4
+ version: 1.5.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Steve Klabnik
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2017-01-05 00:00:00.000000000 Z
11
+ date: 2019-12-20 00:00:00.000000000 Z
13
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rack
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '1.4'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '1.4'
14
27
  - !ruby/object:Gem::Dependency
15
28
  name: rake
16
29
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
30
  requirements:
19
- - - ~>
31
+ - - "~>"
20
32
  - !ruby/object:Gem::Version
21
33
  version: '10.5'
22
34
  type: :development
23
35
  prerelease: false
24
36
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
37
  requirements:
27
- - - ~>
38
+ - - "~>"
28
39
  - !ruby/object:Gem::Version
29
40
  version: '10.5'
30
41
  - !ruby/object:Gem::Dependency
31
42
  name: minitest
32
43
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
44
  requirements:
35
- - - ~>
45
+ - - "~>"
36
46
  - !ruby/object:Gem::Version
37
47
  version: '5.0'
38
48
  type: :development
39
49
  prerelease: false
40
50
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
51
  requirements:
43
- - - ~>
52
+ - - "~>"
44
53
  - !ruby/object:Gem::Version
45
54
  version: '5.0'
46
55
  description: RequestStore gives you per-request global storage.
@@ -50,8 +59,8 @@ executables: []
50
59
  extensions: []
51
60
  extra_rdoc_files: []
52
61
  files:
53
- - .gitignore
54
- - .travis.yml
62
+ - ".gitignore"
63
+ - ".travis.yml"
55
64
  - Gemfile
56
65
  - LICENSE.txt
57
66
  - README.md
@@ -67,33 +76,25 @@ files:
67
76
  homepage: http://github.com/steveklabnik/request_store
68
77
  licenses:
69
78
  - MIT
79
+ metadata: {}
70
80
  post_install_message:
71
81
  rdoc_options: []
72
82
  require_paths:
73
83
  - lib
74
84
  required_ruby_version: !ruby/object:Gem::Requirement
75
- none: false
76
85
  requirements:
77
- - - ! '>='
86
+ - - ">="
78
87
  - !ruby/object:Gem::Version
79
88
  version: '0'
80
- segments:
81
- - 0
82
- hash: 431099191289644974
83
89
  required_rubygems_version: !ruby/object:Gem::Requirement
84
- none: false
85
90
  requirements:
86
- - - ! '>='
91
+ - - ">="
87
92
  - !ruby/object:Gem::Version
88
93
  version: '0'
89
- segments:
90
- - 0
91
- hash: 431099191289644974
92
94
  requirements: []
93
- rubyforge_project:
94
- rubygems_version: 1.8.23
95
+ rubygems_version: 3.0.3
95
96
  signing_key:
96
- specification_version: 3
97
+ specification_version: 4
97
98
  summary: RequestStore gives you per-request global storage.
98
99
  test_files:
99
100
  - test/middleware_test.rb