con_air 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- NzcwMzgzODRiZGE5OTQ2ZmJiMjBhODBjMzkxMzc2OWM3MmYxMGRlZQ==
4
+ YTc4YWYyOTJmNGMwNjE2MjQ5NGY4ODJlZGFiOTE5ZjMzYmVjZDljYw==
5
5
  data.tar.gz: !binary |-
6
- YTc5ZDVlMGE4Mzc2MDNjY2I0YjA0ODczYzc2OTg0NWM1MmFmOTU0Nw==
7
- SHA512:
6
+ OGEzNjE0NGMzYTM0OTcwZGVjZDRiOTM0YmE1NGI0MTdjN2U0MTkxNQ==
7
+ !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- NzU1ZmRmNDk1OTQyOWNiMzA1MWQ1MjEwNDIwYTNhYmM3ZjMwMjE1Y2MwNjc5
10
- YzExMmIyMTMwNGMyNWRiZTY4MmM3OTgzZjExZmZlNjQwN2FiNTdjMzg0NDVh
11
- MzMzYWNlMWE5ZDRiMDEwOWQ4MjZmNjMzMWQxM2IxNDRhOWFkZGM=
9
+ ZWJkOGQ3ODJmZWI1N2VmZDlkMTc5MGQzNTczOTZmOWFiZmQwNTY1MWUwZmU0
10
+ ZWE0MzNhYmJiNzQ3ODllZmQ2MjM3ODBjN2Y4NWY3MmJlZmMxMzlkNTQxYTNm
11
+ OTJkZWY5ZjZkOTMxNTliNTZlMzgxNWM2ZWYzMDViYmU3OGUwOTU=
12
12
  data.tar.gz: !binary |-
13
- ZmMxYjE4MjI3MDY3MDQ0YmYzMGJmM2FmM2M1ZWUzYTRiZGU3YTZmN2I0OTNi
14
- MWIyYjVhNDhmMzMyZWFhYTk2NjUwMWYwOTk5OWFlMzRlYzkwYjViNmJjOWYy
15
- ZTY2NTQ1N2MzMGE3ZjE1ZDFkZTczYjUzYzZkNjRhMWM1ZThmYmQ=
13
+ NTc4NDU2MjQ1Mjk5YjI2OWI2ZjBjODNlNDNkM2FkNzgwN2JhNTVkODQ0MzBi
14
+ MmQ3M2EyOWE4YjkyMzQ1NmU3MDI3ZTQ5Njg3ZGVmNjQyNWMzMmVkOTNkZmYy
15
+ ZTljMzUyNDc1MjUwZTcyOTlhNmQyZDJiOTg4MDU2MDI1NjI2ZmE=
@@ -0,0 +1,27 @@
1
+ module ConAir
2
+ class ConnectionHandler < ActiveRecord::ConnectionAdapters::ConnectionHandler
3
+ attr_accessor :active
4
+ attr_reader :hijacked_spec
5
+ attr_reader :swap_class
6
+
7
+ def initialize(swap_class, hijacked_spec, pools = {})
8
+ super(pools)
9
+
10
+ @hijacked_spec = hijacked_spec
11
+ @swap_class = swap_class
12
+ end
13
+
14
+ def establish_connection(name, spec)
15
+ if name == swap_class.name
16
+ @connection_pools[@hijacked_spec] ||= ActiveRecord::ConnectionAdapters::ConnectionPool.new(@hijacked_spec)
17
+ @class_to_pool[name] = @connection_pools[@hijacked_spec]
18
+ else
19
+ super
20
+ end
21
+ end
22
+
23
+ def exist?(config, klass)
24
+ hijacked_spec.config == config && swap_class == klass
25
+ end
26
+ end
27
+ end
@@ -5,17 +5,23 @@ module ConAir
5
5
  extend ActiveSupport::Concern
6
6
 
7
7
  included do
8
- class_attribute :connection_hijackings
9
- self.connection_hijackings = {}
8
+ class_attribute :handler_hijackings
9
+ self.handler_hijackings = {}
10
10
 
11
11
  class << self
12
- alias_method_chain :connection, :hijacking
12
+ alias_method_chain :connection_handler, :hijacking
13
13
  end
14
14
  end
15
15
 
16
16
  module ClassMethods
17
- def connection_with_hijacking
18
- connection_hijackings[connection_id] || connection_without_hijacking
17
+ def connection_handler_with_hijacking
18
+ handler = handler_hijackings[connection_id]
19
+
20
+ if handler && handler.active
21
+ handler
22
+ else
23
+ connection_handler_without_hijacking
24
+ end
19
25
  end
20
26
  end
21
27
  end
@@ -1,3 +1,3 @@
1
1
  module ConAir
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/con_air.rb CHANGED
@@ -1,13 +1,23 @@
1
1
  require "con_air/version"
2
2
  require "con_air/railtie"
3
+ require "con_air/connection_handler"
3
4
 
4
5
  module ConAir
5
- def self.hijack(conn, &block)
6
+ def self.hijack(config, klass = ActiveRecord::Base, &block)
6
7
  ar = ActiveRecord::Base
7
- ar.connection_hijackings[ar.connection_id] = conn
8
+ handler = ar.handler_hijackings[ar.connection_id]
9
+
10
+ if handler && handler.exist?(config, klass)
11
+ handler.active = true
12
+ else
13
+ spec = ActiveRecord::Base::ConnectionSpecification.new(config, klass.connection_pool.spec.adapter_method)
14
+ ar.handler_hijackings[ar.connection_id] = ConnectionHandler.new(klass, spec)
15
+ end
16
+
17
+ klass.establish_connection
8
18
 
9
19
  yield
10
20
  ensure
11
- ar.connection_hijackings[ar.connection_id] = nil
21
+ ar.handler_hijackings[ar.connection_id].active = false
12
22
  end
13
23
  end
@@ -0,0 +1,49 @@
1
+ require "spec_helper"
2
+
3
+ describe ConAir::ConnectionHandler do
4
+ before do
5
+ @swap_class = double(name: "Swap")
6
+ @config = double
7
+ @spec = double(config: @config)
8
+ @original_spec = double
9
+ @handler = ConAir::ConnectionHandler.new(@swap_class, @spec)
10
+ end
11
+
12
+ context "#establish_connection" do
13
+ context "when class matches the one we want to swap" do
14
+ it "creates pool using passing in spec" do
15
+ expect(ActiveRecord::ConnectionAdapters::ConnectionPool).to receive(:new).with(@spec)
16
+
17
+ @handler.establish_connection(@swap_class.name, @original_spec)
18
+ end
19
+ end
20
+
21
+ context "when class does not match the one we want to swap" do
22
+ it "creates pool using original in spec" do
23
+ expect(ActiveRecord::ConnectionAdapters::ConnectionPool).to receive(:new).with(@original_spec)
24
+
25
+ @handler.establish_connection("SomeClass", @original_spec)
26
+ end
27
+ end
28
+ end
29
+
30
+ context "#exist?" do
31
+ context "when has same config and class to swap" do
32
+ it "returns true" do
33
+ expect(@handler.exist?(@config, @swap_class)).to be(true)
34
+ end
35
+ end
36
+
37
+ context "when has different config" do
38
+ it "returns false" do
39
+ expect(@handler.exist?(double, @swap_class)).to be(false)
40
+ end
41
+ end
42
+
43
+ context "when has different class" do
44
+ it "returns false" do
45
+ expect(@handler.exist?(@config, double)).to be(false)
46
+ end
47
+ end
48
+ end
49
+ end
data/spec/con_air_spec.rb CHANGED
@@ -1,14 +1,94 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe ConAir do
4
- it ".hijack" do
5
- old_connection = ActiveRecord::Base.connection
6
- new_connection = double
4
+ context ".hijack" do
5
+ class DummyHandler
6
+ attr_accessor :active
7
7
 
8
- ConAir.hijack(new_connection) do
9
- expect(ActiveRecord::Base.connection).to eq(new_connection)
8
+ def initialize
9
+ @active = true
10
+ end
10
11
  end
11
12
 
12
- expect(ActiveRecord::Base.connection).to eq(old_connection)
13
+ shared_examples "hijacking connection handler" do |klass|
14
+ before do
15
+ @spec = double
16
+ @new_handler = DummyHandler.new
17
+ @config = double
18
+
19
+ allow(ActiveRecord::Base).to receive(:establish_connection)
20
+ allow(@new_handler).to receive(:hijacked_spec).and_return(double(config: @config))
21
+ allow(ConAir::ConnectionHandler).to receive(:new).with(klass, @spec).and_return(@new_handler)
22
+ allow(ActiveRecord::Base::ConnectionSpecification).to receive(:new).and_return(@spec)
23
+ end
24
+
25
+ after do
26
+ ActiveRecord::Base.handler_hijackings = {}
27
+ end
28
+
29
+ it "switches connection handler" do
30
+ old_handler = ActiveRecord::Base.connection_handler
31
+
32
+ ConAir.hijack(@config, klass) do
33
+ expect(ActiveRecord::Base.connection_handler).to eq(@new_handler)
34
+ end
35
+
36
+ expect(ActiveRecord::Base.connection_handler).to eq(old_handler)
37
+ end
38
+
39
+ it "inactives new handler" do
40
+ ConAir.hijack(@config, klass) { }
41
+
42
+ expect(@new_handler.active).to be(false)
43
+ end
44
+
45
+ it "establish connection" do
46
+ expect(ActiveRecord::Base).to receive(:establish_connection)
47
+
48
+ ConAir.hijack(@config, klass) { }
49
+ end
50
+
51
+ context "when has existing hijacked connection" do
52
+ before do
53
+ allow(@new_handler).to receive(:exist?).and_return(true)
54
+ ConAir.hijack(@config, klass) { }
55
+ end
56
+
57
+ it "sets handler's active and not creating another handler" do
58
+ expect(@new_handler.active).to be(false)
59
+
60
+ ConAir.hijack(@config, klass) do
61
+ expect(@new_handler.active).to be(true)
62
+ end
63
+ end
64
+
65
+ it "does not create handler" do
66
+ expect(ConAir::ConnectionHandler).not_to receive(:new).with(klass, @spec)
67
+
68
+ ConAir.hijack(@config, klass) { }
69
+ end
70
+ end
71
+
72
+ context "when has no existing hijacked connection" do
73
+ before do
74
+ allow(@new_handler).to receive(:exist?).and_return(false)
75
+ ConAir.hijack(@config, klass) { }
76
+ end
77
+
78
+ it "creates connection handler" do
79
+ expect(ConAir::ConnectionHandler).to receive(:new).with(klass, @spec)
80
+
81
+ ConAir.hijack(@config, klass) { }
82
+ end
83
+ end
84
+ end
85
+
86
+ context "when hijacking on subclass of ActiveRecord::Base" do
87
+ it_should_behave_like "hijacking connection handler", User
88
+ end
89
+
90
+ context "when hijacking on ActiveRecord::Base" do
91
+ it_should_behave_like "hijacking connection handler", ActiveRecord::Base
92
+ end
13
93
  end
14
94
  end
metadata CHANGED
@@ -1,95 +1,95 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: con_air
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ngan Pham
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-06 00:00:00.000000000 Z
11
+ date: 2013-12-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- type: :runtime
15
- prerelease: false
16
14
  name: rails
17
- version_requirements: !ruby/object:Gem::Requirement
15
+ requirement: !ruby/object:Gem::Requirement
18
16
  requirements:
19
17
  - - ~>
20
18
  - !ruby/object:Gem::Version
21
19
  version: 3.2.11
22
- requirement: !ruby/object:Gem::Requirement
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ~>
25
25
  - !ruby/object:Gem::Version
26
26
  version: 3.2.11
27
27
  - !ruby/object:Gem::Dependency
28
- type: :development
29
- prerelease: false
30
28
  name: bundler
31
- version_requirements: !ruby/object:Gem::Requirement
29
+ requirement: !ruby/object:Gem::Requirement
32
30
  requirements:
33
31
  - - ~>
34
32
  - !ruby/object:Gem::Version
35
33
  version: '1.3'
36
- requirement: !ruby/object:Gem::Requirement
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - ~>
39
39
  - !ruby/object:Gem::Version
40
40
  version: '1.3'
41
41
  - !ruby/object:Gem::Dependency
42
- type: :development
43
- prerelease: false
44
42
  name: rake
45
- version_requirements: !ruby/object:Gem::Requirement
43
+ requirement: !ruby/object:Gem::Requirement
46
44
  requirements:
47
45
  - - ! '>='
48
46
  - !ruby/object:Gem::Version
49
47
  version: '0'
50
- requirement: !ruby/object:Gem::Requirement
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - ! '>='
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
- type: :development
57
- prerelease: false
58
56
  name: rspec
59
- version_requirements: !ruby/object:Gem::Requirement
57
+ requirement: !ruby/object:Gem::Requirement
60
58
  requirements:
61
59
  - - ~>
62
60
  - !ruby/object:Gem::Version
63
61
  version: 3.0.0.beta1
64
- requirement: !ruby/object:Gem::Requirement
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - ~>
67
67
  - !ruby/object:Gem::Version
68
68
  version: 3.0.0.beta1
69
69
  - !ruby/object:Gem::Dependency
70
- type: :development
71
- prerelease: false
72
70
  name: debugger
73
- version_requirements: !ruby/object:Gem::Requirement
71
+ requirement: !ruby/object:Gem::Requirement
74
72
  requirements:
75
73
  - - ~>
76
74
  - !ruby/object:Gem::Version
77
75
  version: '1.6'
78
- requirement: !ruby/object:Gem::Requirement
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - ~>
81
81
  - !ruby/object:Gem::Version
82
82
  version: '1.6'
83
83
  - !ruby/object:Gem::Dependency
84
- type: :development
85
- prerelease: false
86
84
  name: sqlite3
87
- version_requirements: !ruby/object:Gem::Requirement
85
+ requirement: !ruby/object:Gem::Requirement
88
86
  requirements:
89
87
  - - ! '>='
90
88
  - !ruby/object:Gem::Version
91
89
  version: '0'
92
- requirement: !ruby/object:Gem::Requirement
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
94
  - - ! '>='
95
95
  - !ruby/object:Gem::Version
@@ -101,40 +101,42 @@ executables: []
101
101
  extensions: []
102
102
  extra_rdoc_files: []
103
103
  files:
104
- - lib/con_air/hijacker.rb
105
- - lib/con_air/railtie.rb
106
104
  - lib/con_air/version.rb
105
+ - lib/con_air/railtie.rb
106
+ - lib/con_air/hijacker.rb
107
+ - lib/con_air/connection_handler.rb
107
108
  - lib/con_air.rb
109
+ - spec/con_air/connection_handler_spec.rb
108
110
  - spec/con_air_spec.rb
109
- - spec/dummy/app/assets/javascripts/application.js
110
- - spec/dummy/app/assets/stylesheets/application.css
111
- - spec/dummy/app/controllers/application_controller.rb
112
- - spec/dummy/app/helpers/application_helper.rb
113
- - spec/dummy/app/models/user.rb
114
- - spec/dummy/app/views/layouts/application.html.erb
115
- - spec/dummy/config/application.rb
116
- - spec/dummy/config/boot.rb
117
- - spec/dummy/config/database.yml
111
+ - spec/dummy/Rakefile
112
+ - spec/dummy/README.rdoc
118
113
  - spec/dummy/config/environment.rb
114
+ - spec/dummy/config/locales/en.yml
119
115
  - spec/dummy/config/environments/test.rb
120
- - spec/dummy/config/initializers/backtrace_silencers.rb
121
- - spec/dummy/config/initializers/inflections.rb
122
- - spec/dummy/config/initializers/mime_types.rb
116
+ - spec/dummy/config/application.rb
117
+ - spec/dummy/config/database.yml
118
+ - spec/dummy/config/boot.rb
123
119
  - spec/dummy/config/initializers/secret_token.rb
124
- - spec/dummy/config/initializers/session_store.rb
125
120
  - spec/dummy/config/initializers/wrap_parameters.rb
126
- - spec/dummy/config/locales/en.yml
121
+ - spec/dummy/config/initializers/inflections.rb
122
+ - spec/dummy/config/initializers/session_store.rb
123
+ - spec/dummy/config/initializers/backtrace_silencers.rb
124
+ - spec/dummy/config/initializers/mime_types.rb
127
125
  - spec/dummy/config/routes.rb
128
- - spec/dummy/config.ru
129
126
  - spec/dummy/db/schema.rb
130
- - spec/dummy/lib/soft_deletable_model_callbacks.rb
131
- - spec/dummy/public/404.html
127
+ - spec/dummy/public/favicon.ico
132
128
  - spec/dummy/public/422.html
129
+ - spec/dummy/public/404.html
133
130
  - spec/dummy/public/500.html
134
- - spec/dummy/public/favicon.ico
135
- - spec/dummy/Rakefile
136
- - spec/dummy/README.rdoc
131
+ - spec/dummy/config.ru
137
132
  - spec/dummy/script/rails
133
+ - spec/dummy/app/views/layouts/application.html.erb
134
+ - spec/dummy/app/models/user.rb
135
+ - spec/dummy/app/controllers/application_controller.rb
136
+ - spec/dummy/app/helpers/application_helper.rb
137
+ - spec/dummy/app/assets/stylesheets/application.css
138
+ - spec/dummy/app/assets/javascripts/application.js
139
+ - spec/dummy/lib/soft_deletable_model_callbacks.rb
138
140
  - spec/spec_helper.rb
139
141
  - spec/support/environment.rb
140
142
  - LICENSE.txt
@@ -160,40 +162,41 @@ required_rubygems_version: !ruby/object:Gem::Requirement
160
162
  version: '0'
161
163
  requirements: []
162
164
  rubyforge_project:
163
- rubygems_version: 2.1.9
165
+ rubygems_version: 2.0.6
164
166
  signing_key:
165
167
  specification_version: 4
166
168
  summary: Connection hijacking for ActiveRecord.
167
169
  test_files:
170
+ - spec/con_air/connection_handler_spec.rb
168
171
  - spec/con_air_spec.rb
169
- - spec/dummy/app/assets/javascripts/application.js
170
- - spec/dummy/app/assets/stylesheets/application.css
171
- - spec/dummy/app/controllers/application_controller.rb
172
- - spec/dummy/app/helpers/application_helper.rb
173
- - spec/dummy/app/models/user.rb
174
- - spec/dummy/app/views/layouts/application.html.erb
175
- - spec/dummy/config/application.rb
176
- - spec/dummy/config/boot.rb
177
- - spec/dummy/config/database.yml
172
+ - spec/dummy/Rakefile
173
+ - spec/dummy/README.rdoc
178
174
  - spec/dummy/config/environment.rb
175
+ - spec/dummy/config/locales/en.yml
179
176
  - spec/dummy/config/environments/test.rb
180
- - spec/dummy/config/initializers/backtrace_silencers.rb
181
- - spec/dummy/config/initializers/inflections.rb
182
- - spec/dummy/config/initializers/mime_types.rb
177
+ - spec/dummy/config/application.rb
178
+ - spec/dummy/config/database.yml
179
+ - spec/dummy/config/boot.rb
183
180
  - spec/dummy/config/initializers/secret_token.rb
184
- - spec/dummy/config/initializers/session_store.rb
185
181
  - spec/dummy/config/initializers/wrap_parameters.rb
186
- - spec/dummy/config/locales/en.yml
182
+ - spec/dummy/config/initializers/inflections.rb
183
+ - spec/dummy/config/initializers/session_store.rb
184
+ - spec/dummy/config/initializers/backtrace_silencers.rb
185
+ - spec/dummy/config/initializers/mime_types.rb
187
186
  - spec/dummy/config/routes.rb
188
- - spec/dummy/config.ru
189
187
  - spec/dummy/db/schema.rb
190
- - spec/dummy/lib/soft_deletable_model_callbacks.rb
191
- - spec/dummy/public/404.html
188
+ - spec/dummy/public/favicon.ico
192
189
  - spec/dummy/public/422.html
190
+ - spec/dummy/public/404.html
193
191
  - spec/dummy/public/500.html
194
- - spec/dummy/public/favicon.ico
195
- - spec/dummy/Rakefile
196
- - spec/dummy/README.rdoc
192
+ - spec/dummy/config.ru
197
193
  - spec/dummy/script/rails
194
+ - spec/dummy/app/views/layouts/application.html.erb
195
+ - spec/dummy/app/models/user.rb
196
+ - spec/dummy/app/controllers/application_controller.rb
197
+ - spec/dummy/app/helpers/application_helper.rb
198
+ - spec/dummy/app/assets/stylesheets/application.css
199
+ - spec/dummy/app/assets/javascripts/application.js
200
+ - spec/dummy/lib/soft_deletable_model_callbacks.rb
198
201
  - spec/spec_helper.rb
199
202
  - spec/support/environment.rb