request_store 1.0.2 → 1.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +10 -2
- data/lib/request_store.rb +4 -11
- data/lib/request_store/middleware.rb +12 -0
- data/lib/request_store/version.rb +1 -1
- data/test/middleware_test.rb +15 -0
- data/test/request_store_test.rb +19 -12
- metadata +5 -2
data/README.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
# RequestStore [![build status](https://
|
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/badge.png)](https://codeclimate.com/github/steveklabnik/request_store)
|
2
2
|
|
3
3
|
Ever needed to use a global variable in Rails? Ugh, that's the worst. If you
|
4
|
-
need
|
4
|
+
need global state, you've probably reached for `Thread.current`. Like this:
|
5
5
|
|
6
6
|
```
|
7
7
|
def self.foo
|
@@ -66,6 +66,14 @@ You'll probably have to shove this somewhere:
|
|
66
66
|
use RequestStore::Middleware
|
67
67
|
```
|
68
68
|
|
69
|
+
### No Rack? No Problem!
|
70
|
+
|
71
|
+
If you don't use the Middleware, for example in a raketask or test, you need to do this before first usage:
|
72
|
+
|
73
|
+
```
|
74
|
+
RequestStore.clear!
|
75
|
+
```
|
76
|
+
|
69
77
|
## Contributing
|
70
78
|
|
71
79
|
1. Fork it
|
data/lib/request_store.rb
CHANGED
@@ -1,20 +1,13 @@
|
|
1
1
|
require "request_store/version"
|
2
|
-
|
2
|
+
require "request_store/middleware"
|
3
3
|
require "request_store/railtie" if defined?(Rails)
|
4
4
|
|
5
5
|
module RequestStore
|
6
6
|
def self.store
|
7
|
-
Thread.current[:request_store]
|
7
|
+
Thread.current[:request_store] ||= {}
|
8
8
|
end
|
9
9
|
|
10
|
-
|
11
|
-
|
12
|
-
@app = app
|
13
|
-
end
|
14
|
-
|
15
|
-
def call(env)
|
16
|
-
Thread.current[:request_store] = {}
|
17
|
-
@app.call(env)
|
18
|
-
end
|
10
|
+
def self.clear!
|
11
|
+
Thread.current[:request_store] = {}
|
19
12
|
end
|
20
13
|
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
|
3
|
+
require 'request_store'
|
4
|
+
|
5
|
+
class MiddlewareTest < Minitest::Unit::TestCase
|
6
|
+
def test_middleware_resets_store
|
7
|
+
app = RackApp.new
|
8
|
+
middleware = RequestStore::Middleware.new(app)
|
9
|
+
|
10
|
+
middleware.call({})
|
11
|
+
middleware.call({})
|
12
|
+
|
13
|
+
assert_equal 1, RequestStore.store[:foo]
|
14
|
+
end
|
15
|
+
end
|
data/test/request_store_test.rb
CHANGED
@@ -3,26 +3,33 @@ require 'minitest/autorun'
|
|
3
3
|
require 'request_store'
|
4
4
|
|
5
5
|
class RequestStoreTest < Minitest::Unit::TestCase
|
6
|
+
def test_initial_state
|
7
|
+
Thread.current[:request_store] = nil
|
8
|
+
assert_equal RequestStore.store, Hash.new
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_init_with_hash
|
12
|
+
RequestStore.clear!
|
13
|
+
assert_equal Hash.new, RequestStore.store
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_clear
|
17
|
+
RequestStore.clear!
|
18
|
+
RequestStore.store[:foo] = 1
|
19
|
+
RequestStore.clear!
|
20
|
+
assert_equal Hash.new, RequestStore.store
|
21
|
+
end
|
22
|
+
|
6
23
|
def test_quacks_like_hash
|
24
|
+
RequestStore.clear!
|
7
25
|
RequestStore.store[:foo] = 1
|
8
26
|
assert_equal 1, RequestStore.store[:foo]
|
9
27
|
assert_equal 1, RequestStore.store.fetch(:foo)
|
10
28
|
end
|
11
29
|
|
12
30
|
def test_delegates_to_thread
|
31
|
+
RequestStore.clear!
|
13
32
|
RequestStore.store[:foo] = 1
|
14
33
|
assert_equal 1, Thread.current[:request_store][:foo]
|
15
34
|
end
|
16
35
|
end
|
17
|
-
|
18
|
-
class MiddlewareTest < Minitest::Unit::TestCase
|
19
|
-
def test_middleware_resets_store
|
20
|
-
app = RackApp.new
|
21
|
-
middleware = RequestStore::Middleware.new(app)
|
22
|
-
|
23
|
-
middleware.call({})
|
24
|
-
middleware.call({})
|
25
|
-
|
26
|
-
assert_equal 1, Thread.current[:request_store][:foo]
|
27
|
-
end
|
28
|
-
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: request_store
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-12-
|
12
|
+
date: 2012-12-27 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
@@ -57,9 +57,11 @@ files:
|
|
57
57
|
- README.md
|
58
58
|
- Rakefile
|
59
59
|
- lib/request_store.rb
|
60
|
+
- lib/request_store/middleware.rb
|
60
61
|
- lib/request_store/railtie.rb
|
61
62
|
- lib/request_store/version.rb
|
62
63
|
- request_store.gemspec
|
64
|
+
- test/middleware_test.rb
|
63
65
|
- test/request_store_test.rb
|
64
66
|
- test/test_helper.rb
|
65
67
|
homepage: http://github.com/steveklabnik/request_store
|
@@ -87,6 +89,7 @@ signing_key:
|
|
87
89
|
specification_version: 3
|
88
90
|
summary: RequestStore gives you per-request global storage.
|
89
91
|
test_files:
|
92
|
+
- test/middleware_test.rb
|
90
93
|
- test/request_store_test.rb
|
91
94
|
- test/test_helper.rb
|
92
95
|
has_rdoc:
|