activerecord-connections 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
@@ -1,8 +1,9 @@
|
|
1
|
+
require 'active_support'
|
1
2
|
require 'active_support/concern'
|
2
3
|
|
3
4
|
module ActiveRecord
|
4
5
|
module Connections
|
5
|
-
autoload :
|
6
|
+
autoload :ConnectionProxy, 'active_record/connections/connection_proxy'
|
6
7
|
|
7
8
|
extend ActiveSupport::Concern
|
8
9
|
|
@@ -33,7 +34,7 @@ module ActiveRecord
|
|
33
34
|
# end
|
34
35
|
#
|
35
36
|
def using_connection(connection_name, connection_spec)
|
36
|
-
self.proxy_connection =
|
37
|
+
self.proxy_connection = ConnectionProxy.new(connection_name, connection_spec)
|
37
38
|
|
38
39
|
def self.connection_pool
|
39
40
|
connection_handler.retrieve_connection_pool(proxy_connection)
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'active_support/basic_object'
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
module Connections
|
5
|
+
class ConnectionProxy < ActiveSupport::BasicObject
|
6
|
+
def initialize(connection_name, connection_spec)
|
7
|
+
@connection_name, @connection_spec = connection_name, connection_spec
|
8
|
+
end
|
9
|
+
|
10
|
+
def respond_to?(method_name, include_private = false)
|
11
|
+
connection.respond_to?(method_name, include_private)
|
12
|
+
end
|
13
|
+
|
14
|
+
def method_missing(method_name, *arguments, &block)
|
15
|
+
connection.send(method_name, *arguments, &block)
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def connection
|
21
|
+
@connection ||= retrieve_connection_klass rescue fabricate_connection_klass
|
22
|
+
end
|
23
|
+
|
24
|
+
def retrieve_connection_klass
|
25
|
+
"ActiveRecord::Connections::AbstractConnection#{@connection_name}".constantize
|
26
|
+
end
|
27
|
+
|
28
|
+
def fabricate_connection_klass
|
29
|
+
::ActiveRecord::Connections.class_eval <<-RUBY
|
30
|
+
class AbstractConnection#{@connection_name} < ActiveRecord::Base
|
31
|
+
self.abstract_class = true
|
32
|
+
self.establish_connection(#{@connection_spec.inspect})
|
33
|
+
|
34
|
+
self
|
35
|
+
end
|
36
|
+
RUBY
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: activerecord-connections
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0.
|
5
|
+
version: 0.0.2
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Gabriel Sobrinho
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-08-
|
13
|
+
date: 2011-08-24 00:00:00 -03:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
@@ -96,7 +96,7 @@ files:
|
|
96
96
|
- Rakefile
|
97
97
|
- activerecord-connections.gemspec
|
98
98
|
- lib/active_record/connections.rb
|
99
|
-
- lib/active_record/connections/
|
99
|
+
- lib/active_record/connections/connection_proxy.rb
|
100
100
|
- lib/activerecord-connections.rb
|
101
101
|
- test/active_record/connections_test.rb
|
102
102
|
- test/db/migrate/20110822114329_create_customers.rb
|
@@ -1,43 +0,0 @@
|
|
1
|
-
#
|
2
|
-
# This class is used to automatically generate small abstract ActiveRecord classes
|
3
|
-
# that would then be used as a source of database connections for ActiveRecord::Connections.
|
4
|
-
# This way we do not need to re-implement all the connection establishing code
|
5
|
-
# that ActiveRecord already has and we make our code less dependant on Rails versions.
|
6
|
-
#
|
7
|
-
module ActiveRecord
|
8
|
-
module Connections
|
9
|
-
class ConnectionFactory
|
10
|
-
cattr_accessor :connection_classes
|
11
|
-
self.connection_classes = {}
|
12
|
-
|
13
|
-
# Establishes connection or return an existing one from cache
|
14
|
-
def self.establish_connection(connection_name, connection_spec)
|
15
|
-
connection_classes[connection_name] ||= make_connection_klass(connection_name, connection_spec)
|
16
|
-
end
|
17
|
-
|
18
|
-
protected
|
19
|
-
|
20
|
-
# Generate an abstract AR class with specified connection established
|
21
|
-
def self.make_connection_klass(connection_name, connection_spec)
|
22
|
-
# Generate class
|
23
|
-
klass = generate_connection_klass(connection_name)
|
24
|
-
|
25
|
-
# Establish connection
|
26
|
-
klass.establish_connection(connection_spec)
|
27
|
-
|
28
|
-
# Return the class
|
29
|
-
return klass
|
30
|
-
end
|
31
|
-
|
32
|
-
def self.generate_connection_klass(connection_name)
|
33
|
-
class_eval <<-RUBY
|
34
|
-
class AbstractConnection#{connection_name} < ActiveRecord::Base
|
35
|
-
self.abstract_class = true
|
36
|
-
|
37
|
-
self
|
38
|
-
end
|
39
|
-
RUBY
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|