rack-with_sequel 0.0.1

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,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4009b0ba6d406825e1dbb972760dd4cbddd47b8e
4
+ data.tar.gz: f8efc2b1e3fa1445f2b34bf93f84b3eb3df985f6
5
+ SHA512:
6
+ metadata.gz: 9f90bc8a1e1fb5696a685ccd487034078667bc95d16f62ac87aae7b968a4573aa685a335dc28981abd4c181ddafe8bc351d5adb4b1d60e6abbb087f09802c4ae
7
+ data.tar.gz: 27682c659d0b3a2dc398280fa9f3345007f4cc133f377441bd96df437645dc878c16c37c0569906163410823af9a1cb0eb043071964280008d0eee44f6ebaba9
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rack-with_sequel.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Vladimir Kochnev
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,86 @@
1
+ # Rack::WithSequel
2
+
3
+ Rack middleware that explicitely acquires Sequel database connection for entire request. It's reasonable if you want a Rails-like behaviour where database connection is released by the `ActiveRecord::ConnectionAdapters::ConnectionManagement` middleware at the end of request. But please use it with **warning** that using `Rack::WithSequel` you get **every** request locking one connection from pool even if database is not used during this request.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'rack-with_sequel'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install rack-with_sequel
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ use Rack::WithSequel
25
+ ```
26
+
27
+ By default it will wrap every request in `Sequel::Model.db.synchronize do ... end`.
28
+
29
+ Suppose we have a PostgreSQL connection somewhere:
30
+
31
+ ```ruby
32
+ DB = Sequel.connect("postgres://postgres:postgres@localhost:5432/test")
33
+ ```
34
+
35
+ Then we can pass it to the middleware as an option:
36
+
37
+ ```ruby
38
+ use Rack::WithSequel, db: DB
39
+ ```
40
+
41
+ So `DB` will be used instead of `Sequel::Model.db`.
42
+
43
+ Minimal example:
44
+
45
+ ```ruby
46
+ require 'sinatra'
47
+ require 'rack/with_sequel'
48
+
49
+ db = Sequel.connect("postgresql://postgres:postgres@localhost:5432/test")
50
+
51
+ use Rack::WithSequel, db: db
52
+
53
+ get '/' do
54
+ puts db[:test].all.inspect
55
+ end
56
+ ```
57
+
58
+ ## Handling connection errors
59
+
60
+ Sometimes it's reasonable to handle connection errors. In this case it's recommended to define a custom middleware:
61
+
62
+ ```ruby
63
+ require 'rack/with_sequel'
64
+
65
+ class CustomWithSequel < Rack::WithSequel
66
+ ERRORS = [Sequel::DatabaseConnectionError, Sequel::PoolTimeout]
67
+
68
+ def call(env)
69
+ super
70
+ rescue *ERRORS
71
+ [503, {}, ["database connection error"]]
72
+ end
73
+ end
74
+
75
+ # ...
76
+
77
+ use CustomWithSequel
78
+ ```
79
+
80
+ ## Contributing
81
+
82
+ 1. Fork it ( https://github.com/marshall-lee/rack-with_sequel/fork )
83
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
84
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
85
+ 4. Push to the branch (`git push origin my-new-feature`)
86
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,19 @@
1
+ module Rack
2
+ class WithSequel
3
+ def initialize(app, options={})
4
+ @app = app
5
+ @db = options[:db]
6
+ end
7
+
8
+ def call(env)
9
+ db.synchronize do
10
+ @app.call(env)
11
+ end
12
+ end
13
+
14
+ private
15
+ def db
16
+ @db ||= Sequel::Model.db
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "rack-with_sequel"
7
+ spec.version = "0.0.1"
8
+ spec.authors = ["Vladimir Kochnev"]
9
+ spec.email = ["hashtable@yandex.ru"]
10
+ spec.summary = %q{Explicitely acquires a Sequel connection from the pool per request}
11
+ spec.description = %q{Rack middleware that explicitely acquires a Sequel connection from the pool per request}
12
+ spec.homepage = "https://github.com/marshall-lee/rack-with_sequel"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.7"
21
+ spec.add_development_dependency "rake", "~> 10.0"
22
+
23
+ spec.add_runtime_dependency "rack"
24
+ spec.add_runtime_dependency "sequel", ">= 2.5.0"
25
+ end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-with_sequel
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Vladimir Kochnev
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rack
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: sequel
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 2.5.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: 2.5.0
69
+ description: Rack middleware that explicitely acquires a Sequel connection from the
70
+ pool per request
71
+ email:
72
+ - hashtable@yandex.ru
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - lib/rack/with_sequel.rb
83
+ - rack-with_sequel.gemspec
84
+ homepage: https://github.com/marshall-lee/rack-with_sequel
85
+ licenses:
86
+ - MIT
87
+ metadata: {}
88
+ post_install_message:
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubyforge_project:
104
+ rubygems_version: 2.4.5
105
+ signing_key:
106
+ specification_version: 4
107
+ summary: Explicitely acquires a Sequel connection from the pool per request
108
+ test_files: []
109
+ has_rdoc: