db-charmer 1.5.1 → 1.5.2
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.
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.5.
|
1
|
+
1.5.2
|
data/db-charmer.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{db-charmer}
|
8
|
-
s.version = "1.5.
|
8
|
+
s.version = "1.5.2"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Alexey Kovyrin"]
|
12
|
-
s.date = %q{2010-03-
|
12
|
+
s.date = %q{2010-03-10}
|
13
13
|
s.description = %q{ActiveRecord Connections Magic (slaves, multiple connections, etc)}
|
14
14
|
s.email = %q{alexey@kovyrin.net}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -6,15 +6,13 @@ module DbCharmer
|
|
6
6
|
end
|
7
7
|
|
8
8
|
def connection_name
|
9
|
-
@
|
10
|
-
|
11
|
-
|
12
|
-
def connection_name=(name)
|
13
|
-
@connection_name = name
|
9
|
+
raise "Can't find connection configuration!" unless @config
|
10
|
+
@config[:connection_name]
|
14
11
|
end
|
15
12
|
|
16
13
|
def format_log_entry_with_connection_name(message, dump = nil)
|
17
14
|
msg = connection_name ? "[#{connection_name}] " : ''
|
15
|
+
msg = " \e[0;34;1m#{msg}\e[0m" if connection_name && ActiveRecord::Base.colorize_logging
|
18
16
|
msg << format_log_entry_without_connection_name(message, dump)
|
19
17
|
end
|
20
18
|
end
|
@@ -7,8 +7,11 @@ module DbCharmer
|
|
7
7
|
if should_exist && !config
|
8
8
|
raise ArgumentError, "Invalid connection name (does not exist in database.yml): #{RAILS_ENV}/#{name}"
|
9
9
|
end
|
10
|
-
|
11
|
-
|
10
|
+
# If there is no config, use default one
|
11
|
+
config ||= configurations[RAILS_ENV].clone
|
12
|
+
# Pass connection name with config
|
13
|
+
config[:connection_name] = name.to_s
|
14
|
+
establish_connection(config)
|
12
15
|
end
|
13
16
|
|
14
17
|
#-----------------------------------------------------------------------------
|
@@ -1,38 +1,46 @@
|
|
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 DbCharmer magic.
|
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
|
+
#
|
1
7
|
module DbCharmer
|
2
|
-
|
8
|
+
module ConnectionFactory
|
3
9
|
@@connection_classes = {}
|
4
10
|
|
5
11
|
def self.reset!
|
6
12
|
@@connection_classes = {}
|
7
13
|
end
|
8
14
|
|
15
|
+
# Establishes connection or return an existing one from cache
|
9
16
|
def self.connect(db_name, should_exist = false)
|
10
|
-
|
11
|
-
|
12
|
-
# return @@connection_classes[db_name.to_s]
|
17
|
+
db_name = db_name.to_s
|
18
|
+
@@connection_classes[db_name] ||= establish_connection(db_name, should_exist)
|
13
19
|
end
|
14
20
|
|
21
|
+
# Establish connection with a specified name
|
15
22
|
def self.establish_connection(db_name, should_exist = false)
|
16
|
-
# DbCharmer.logger.debug("Creating a connection proxy for #{db_name}")
|
17
23
|
abstract_class = generate_abstract_class(db_name, should_exist)
|
18
24
|
DbCharmer::ConnectionProxy.new(abstract_class)
|
19
25
|
end
|
20
26
|
|
27
|
+
# Generate an abstract AR class with specified connection established
|
21
28
|
def self.generate_abstract_class(db_name, should_exist = false)
|
22
|
-
|
23
|
-
|
29
|
+
klass = abstract_connection_class_name(db_name)
|
30
|
+
# Generate class
|
24
31
|
module_eval <<-EOF, __FILE__, __LINE__ + 1
|
25
|
-
class #{
|
32
|
+
class #{klass} < ActiveRecord::Base
|
26
33
|
self.abstract_class = true
|
27
34
|
establish_real_connection_if_exists(:#{db_name}, #{!!should_exist})
|
28
35
|
end
|
29
36
|
EOF
|
30
|
-
|
31
|
-
|
37
|
+
# Return class
|
38
|
+
klass.constantize
|
32
39
|
end
|
33
40
|
|
41
|
+
# Generates unique names for our abstract AR classes
|
34
42
|
def self.abstract_connection_class_name(db_name)
|
35
|
-
"::AutoGeneratedAbstractConnectionClass#{db_name.camelize}"
|
43
|
+
"::AutoGeneratedAbstractConnectionClass#{db_name.to_s.camelize}"
|
36
44
|
end
|
37
45
|
end
|
38
|
-
end
|
46
|
+
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 1
|
7
7
|
- 5
|
8
|
-
-
|
9
|
-
version: 1.5.
|
8
|
+
- 2
|
9
|
+
version: 1.5.2
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Alexey Kovyrin
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-03-
|
17
|
+
date: 2010-03-10 00:00:00 -05:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|