request_store 1.0.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.
@@ -0,0 +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
@@ -0,0 +1,9 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 1.9.2
5
+ - rbx-19mode
6
+ - jruby-19mode
7
+ - ruby-head
8
+ - 1.8.7
9
+ - ree
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in request_store.gemspec
4
+ gemspec
@@ -0,0 +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
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,77 @@
1
+ # RequestStore [![build status](https://secure.travis-ci.org/steveklabnik/request_store.png?branch=master)](https://secure.travis-ci.org/steveklabnik/request_store) [![Code Climate](https://codeclimate.com/badge.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 gobal state, you've probably reached for `Thread.current`. Like this:
5
+
6
+ ```
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
+ ```
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
+ gem 'request_store'
43
+
44
+ And change the code to this:
45
+
46
+ ```
47
+ def index
48
+ RequestStore.store[:foo] ||= 0
49
+ RequestStore.store[:foo] += 1
50
+
51
+ render :text => RequestStore.store[:foo]
52
+ end
53
+ ```
54
+
55
+ Yep, everywhere you used `Thread.current` just change it to
56
+ `RequestStore.store`. Now no matter what server you use, you'll get `1` every
57
+ time: the storage is local to that request.
58
+
59
+ ### No Rails? No Problem!
60
+
61
+ A Railtie is added that configures the Middleware for you, but if you're not
62
+ using Rails, no biggie! Just use the Middelware yourself, however you need.
63
+ You'll probably have to shove this somewhere:
64
+
65
+ ```
66
+ use RequestStore::Middleware
67
+ ```
68
+
69
+ ## Contributing
70
+
71
+ 1. Fork it
72
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
73
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
74
+ 4. Push to the branch (`git push origin my-new-feature`)
75
+ 5. Create new Pull Request
76
+
77
+ Don't forget to run the tests with `rake`.
@@ -0,0 +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
@@ -0,0 +1,21 @@
1
+ require "request_store/version"
2
+
3
+ require "request_store/railtie" if defined?(Rails)
4
+
5
+ module RequestStore
6
+ def self.store
7
+ Thread.current[:request_store] ||= {}
8
+ Thread.current[:request_store]
9
+ end
10
+
11
+ class Middleware
12
+ def initialize(app)
13
+ @app = app
14
+ end
15
+
16
+ def call(env)
17
+ Thread.current[:request_store] = {}
18
+ @app.call(env)
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,7 @@
1
+ module RequestStore
2
+ class Railtie < Rails::Railtie
3
+ initializer "request_store.insert_middleware" do |app|
4
+ app.config.middleware.use RequestStore::Middleware
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module RequestStore
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,22 @@
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
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_development_dependency "rake"
21
+ gem.add_development_dependency 'minitest', '~> 3.0'
22
+ end
@@ -0,0 +1,28 @@
1
+ require 'minitest/autorun'
2
+
3
+ require 'request_store'
4
+
5
+ class RequestStoreTest < Minitest::Unit::TestCase
6
+ def test_quacks_like_hash
7
+ RequestStore.store[:foo] = 1
8
+ assert_equal 1, RequestStore.store[:foo]
9
+ assert_equal 1, RequestStore.store.fetch(:foo)
10
+ end
11
+
12
+ def test_delegates_to_thread
13
+ RequestStore.store[:foo] = 1
14
+ assert_equal 1, Thread.current[:request_store][:foo]
15
+ end
16
+ 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
@@ -0,0 +1,6 @@
1
+ class RackApp
2
+ def call(env)
3
+ RequestStore.store[:foo] ||= 0
4
+ RequestStore.store[:foo] += 1
5
+ end
6
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: request_store
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Steve Klabnik
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-17 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: minitest
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '3.0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '3.0'
46
+ description: RequestStore gives you per-request global storage.
47
+ email:
48
+ - steve@steveklabnik.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - .travis.yml
55
+ - Gemfile
56
+ - LICENSE.txt
57
+ - README.md
58
+ - Rakefile
59
+ - lib/request_store.rb
60
+ - lib/request_store/railtie.rb
61
+ - lib/request_store/version.rb
62
+ - request_store.gemspec
63
+ - test/request_store_test.rb
64
+ - test/test_helper.rb
65
+ homepage: http://github.com/steveklabnik/request_store
66
+ licenses: []
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubyforge_project:
85
+ rubygems_version: 1.8.23
86
+ signing_key:
87
+ specification_version: 3
88
+ summary: RequestStore gives you per-request global storage.
89
+ test_files:
90
+ - test/request_store_test.rb
91
+ - test/test_helper.rb
92
+ has_rdoc: