request_store 1.0.5 → 1.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.travis.yml +11 -3
- data/README.md +9 -7
- data/lib/request_store.rb +31 -6
- data/lib/request_store/middleware.rb +2 -1
- data/lib/request_store/railtie.rb +1 -1
- data/lib/request_store/version.rb +1 -1
- data/test/middleware_test.rb +19 -5
- data/test/request_store_test.rb +21 -0
- data/test/test_helper.rb +4 -0
- metadata +13 -20
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: df9be17564fafde0934eac75799b5180b42d34cf
|
4
|
+
data.tar.gz: d0e4d1969cdc9a15f82b04e34922e6b8b08fb83f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ff57c6bc11afb5bf2526e0a03046b56aa544eeaa9b54a97e0c79c7a85c0c1fffeca9872ec5ff7965d33651dacc728c32aa12a7b657a7baf3a4dbcdc595f1d898
|
7
|
+
data.tar.gz: 755394f8fae9ae418599a2b453037b1e8da11a7a9c245de6387cb533ad0cb89eca3b5cd72afc59286f6b26121620d1e537ebecd82d7ef9571f6a4ab07c9fb3ed
|
data/.travis.yml
CHANGED
@@ -1,9 +1,17 @@
|
|
1
1
|
language: ruby
|
2
2
|
rvm:
|
3
|
-
- 1.
|
3
|
+
- 1.8.7
|
4
4
|
- 1.9.2
|
5
|
-
-
|
5
|
+
- 1.9.3
|
6
|
+
- 2.0.0
|
7
|
+
- 2.1.0
|
8
|
+
- jruby-18mode
|
6
9
|
- jruby-19mode
|
10
|
+
- rbx-2
|
7
11
|
- ruby-head
|
8
|
-
-
|
12
|
+
- jruby-head
|
9
13
|
- ree
|
14
|
+
matrix:
|
15
|
+
allow_failures:
|
16
|
+
- rvm: ruby-head
|
17
|
+
- rvm: jruby-head
|
data/README.md
CHANGED
@@ -1,9 +1,9 @@
|
|
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/
|
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
2
|
|
3
3
|
Ever needed to use a global variable in Rails? Ugh, that's the worst. If you
|
4
4
|
need global state, you've probably reached for `Thread.current`. Like this:
|
5
5
|
|
6
|
-
```
|
6
|
+
```ruby
|
7
7
|
def self.foo
|
8
8
|
Thread.current[:foo] ||= 0
|
9
9
|
end
|
@@ -23,7 +23,7 @@ and you use one of those servers, watch out! Values can stick around longer
|
|
23
23
|
than you'd expect, and this can cause bugs. For example, if we had this in
|
24
24
|
our controller:
|
25
25
|
|
26
|
-
```
|
26
|
+
```ruby
|
27
27
|
def index
|
28
28
|
Thread.current[:counter] ||= 0
|
29
29
|
Thread.current[:counter] += 1
|
@@ -39,11 +39,13 @@ you run it with Thin, you get `1`, then `2`, then `3`...
|
|
39
39
|
|
40
40
|
Add this line to your application's Gemfile:
|
41
41
|
|
42
|
-
|
42
|
+
```ruby
|
43
|
+
gem 'request_store'
|
44
|
+
```
|
43
45
|
|
44
46
|
And change the code to this:
|
45
47
|
|
46
|
-
```
|
48
|
+
```ruby
|
47
49
|
def index
|
48
50
|
RequestStore.store[:foo] ||= 0
|
49
51
|
RequestStore.store[:foo] += 1
|
@@ -63,7 +65,7 @@ apps, but if your app is tied to an older (2.x) version, you will have to
|
|
63
65
|
manually add the middleware yourself. Typically this should just be a matter
|
64
66
|
of adding:
|
65
67
|
|
66
|
-
```
|
68
|
+
```ruby
|
67
69
|
config.middleware.use RequestStore::Middleware
|
68
70
|
```
|
69
71
|
|
@@ -75,7 +77,7 @@ A Railtie is added that configures the Middleware for you, but if you're not
|
|
75
77
|
using Rails, no biggie! Just use the Middleware yourself, however you need.
|
76
78
|
You'll probably have to shove this somewhere:
|
77
79
|
|
78
|
-
```
|
80
|
+
```ruby
|
79
81
|
use RequestStore::Middleware
|
80
82
|
```
|
81
83
|
|
data/lib/request_store.rb
CHANGED
@@ -3,11 +3,36 @@ require "request_store/middleware"
|
|
3
3
|
require "request_store/railtie" if defined?(Rails::Railtie)
|
4
4
|
|
5
5
|
module RequestStore
|
6
|
-
|
7
|
-
|
8
|
-
|
6
|
+
def self.store
|
7
|
+
Thread.current[:request_store] ||= {}
|
8
|
+
end
|
9
9
|
|
10
|
-
|
11
|
-
|
12
|
-
|
10
|
+
def self.clear!
|
11
|
+
Thread.current[:request_store] = {}
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.read(key)
|
15
|
+
store[key]
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.[](key)
|
19
|
+
store[key]
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.write(key, value)
|
23
|
+
store[key] = value
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.[]=(key, value)
|
27
|
+
store[key] = value
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.exist?(key)
|
31
|
+
store.key?(key)
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.fetch(key, &block)
|
35
|
+
store[key] = yield unless exist?(key)
|
36
|
+
store[key]
|
37
|
+
end
|
13
38
|
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
module RequestStore
|
2
2
|
class Railtie < ::Rails::Railtie
|
3
3
|
initializer "request_store.insert_middleware" do |app|
|
4
|
-
app.config.middleware.
|
4
|
+
app.config.middleware.insert_after Rack::MethodOverride, RequestStore::Middleware
|
5
5
|
end
|
6
6
|
end
|
7
7
|
end
|
data/test/middleware_test.rb
CHANGED
@@ -3,13 +3,27 @@ require 'minitest/autorun'
|
|
3
3
|
require 'request_store'
|
4
4
|
|
5
5
|
class MiddlewareTest < Minitest::Unit::TestCase
|
6
|
+
def setup
|
7
|
+
@app = RackApp.new
|
8
|
+
@middleware = RequestStore::Middleware.new(@app)
|
9
|
+
end
|
10
|
+
|
6
11
|
def test_middleware_resets_store
|
7
|
-
|
8
|
-
|
12
|
+
2.times { @middleware.call({}) }
|
13
|
+
|
14
|
+
assert_equal 1, @app.last_value
|
15
|
+
assert_equal({}, RequestStore.store)
|
16
|
+
end
|
9
17
|
|
10
|
-
|
11
|
-
|
18
|
+
def test_middleware_resets_store_on_error
|
19
|
+
errors = []
|
20
|
+
begin
|
21
|
+
@middleware.call({:error => true})
|
22
|
+
rescue => e
|
23
|
+
errors << e
|
24
|
+
end
|
12
25
|
|
13
|
-
assert_equal
|
26
|
+
assert_equal ['FAIL'], errors.map(&:message)
|
27
|
+
assert_equal({}, RequestStore.store)
|
14
28
|
end
|
15
29
|
end
|
data/test/request_store_test.rb
CHANGED
@@ -27,6 +27,27 @@ class RequestStoreTest < Minitest::Unit::TestCase
|
|
27
27
|
assert_equal 1, RequestStore.store.fetch(:foo)
|
28
28
|
end
|
29
29
|
|
30
|
+
def test_read
|
31
|
+
RequestStore.clear!
|
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.clear!
|
39
|
+
RequestStore.write(:foo, 1)
|
40
|
+
assert_equal 1, RequestStore.store[:foo]
|
41
|
+
RequestStore[:foo] = 2
|
42
|
+
assert_equal 2, RequestStore.store[:foo]
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_fetch
|
46
|
+
RequestStore.clear!
|
47
|
+
assert_equal 2, RequestStore.fetch(:foo) { 1 + 1 }
|
48
|
+
assert_equal 2, RequestStore.fetch(:foo) { 2 + 2 }
|
49
|
+
end
|
50
|
+
|
30
51
|
def test_delegates_to_thread
|
31
52
|
RequestStore.clear!
|
32
53
|
RequestStore.store[:foo] = 1
|
data/test/test_helper.rb
CHANGED
metadata
CHANGED
@@ -1,46 +1,41 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: request_store
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
5
|
-
prerelease:
|
4
|
+
version: 1.0.6
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Steve Klabnik
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2014-06-10 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: rake
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
|
-
- -
|
17
|
+
- - ">="
|
20
18
|
- !ruby/object:Gem::Version
|
21
19
|
version: '0'
|
22
20
|
type: :development
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
|
-
- -
|
24
|
+
- - ">="
|
28
25
|
- !ruby/object:Gem::Version
|
29
26
|
version: '0'
|
30
27
|
- !ruby/object:Gem::Dependency
|
31
28
|
name: minitest
|
32
29
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
30
|
requirements:
|
35
|
-
- - ~>
|
31
|
+
- - "~>"
|
36
32
|
- !ruby/object:Gem::Version
|
37
33
|
version: '3.0'
|
38
34
|
type: :development
|
39
35
|
prerelease: false
|
40
36
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
37
|
requirements:
|
43
|
-
- - ~>
|
38
|
+
- - "~>"
|
44
39
|
- !ruby/object:Gem::Version
|
45
40
|
version: '3.0'
|
46
41
|
description: RequestStore gives you per-request global storage.
|
@@ -50,8 +45,8 @@ executables: []
|
|
50
45
|
extensions: []
|
51
46
|
extra_rdoc_files: []
|
52
47
|
files:
|
53
|
-
- .gitignore
|
54
|
-
- .travis.yml
|
48
|
+
- ".gitignore"
|
49
|
+
- ".travis.yml"
|
55
50
|
- Gemfile
|
56
51
|
- LICENSE.txt
|
57
52
|
- README.md
|
@@ -66,30 +61,28 @@ files:
|
|
66
61
|
- test/test_helper.rb
|
67
62
|
homepage: http://github.com/steveklabnik/request_store
|
68
63
|
licenses: []
|
64
|
+
metadata: {}
|
69
65
|
post_install_message:
|
70
66
|
rdoc_options: []
|
71
67
|
require_paths:
|
72
68
|
- lib
|
73
69
|
required_ruby_version: !ruby/object:Gem::Requirement
|
74
|
-
none: false
|
75
70
|
requirements:
|
76
|
-
- -
|
71
|
+
- - ">="
|
77
72
|
- !ruby/object:Gem::Version
|
78
73
|
version: '0'
|
79
74
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
80
|
-
none: false
|
81
75
|
requirements:
|
82
|
-
- -
|
76
|
+
- - ">="
|
83
77
|
- !ruby/object:Gem::Version
|
84
78
|
version: '0'
|
85
79
|
requirements: []
|
86
80
|
rubyforge_project:
|
87
|
-
rubygems_version:
|
81
|
+
rubygems_version: 2.2.2
|
88
82
|
signing_key:
|
89
|
-
specification_version:
|
83
|
+
specification_version: 4
|
90
84
|
summary: RequestStore gives you per-request global storage.
|
91
85
|
test_files:
|
92
86
|
- test/middleware_test.rb
|
93
87
|
- test/request_store_test.rb
|
94
88
|
- test/test_helper.rb
|
95
|
-
has_rdoc:
|