db-charmer 1.3.4 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.3.4
1
+ 1.4.0
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{db-charmer}
8
- s.version = "1.3.4"
8
+ s.version = "1.4.0"
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{2009-10-28}
12
+ s.date = %q{2009-11-02}
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 = [
@@ -27,7 +27,6 @@ Gem::Specification.new do |s|
27
27
  "init.rb",
28
28
  "lib/db_charmer.rb",
29
29
  "lib/db_charmer/active_record_extensions.rb",
30
- "lib/db_charmer/association_proxy.rb",
31
30
  "lib/db_charmer/connection_factory.rb",
32
31
  "lib/db_charmer/connection_proxy.rb",
33
32
  "lib/db_charmer/connection_switch.rb",
@@ -14,20 +14,29 @@ module DbCharmer
14
14
  def self.connections_should_exist?
15
15
  !! connections_should_exist
16
16
  end
17
-
17
+
18
18
  def self.logger
19
19
  return Rails.logger if defined?(Rails)
20
20
  @logger ||= Logger.new(STDERR)
21
21
  end
22
22
  end
23
23
 
24
+ class Object
25
+ def self.proxy?
26
+ false
27
+ end
28
+
29
+ def proxy?
30
+ false
31
+ end
32
+ end
33
+
24
34
  puts "Extending AR..."
25
35
 
26
36
  require 'db_charmer/active_record_extensions'
27
37
  require 'db_charmer/connection_factory'
28
38
  require 'db_charmer/connection_proxy'
29
39
  require 'db_charmer/connection_switch'
30
- require 'db_charmer/association_proxy'
31
40
  require 'db_charmer/scope_proxy'
32
41
  require 'db_charmer/multi_db_proxy'
33
42
 
@@ -40,12 +49,36 @@ ActiveRecord::Base.extend(DbCharmer::ConnectionSwitch::ClassMethods)
40
49
  # Enable connection proxy in AR
41
50
  ActiveRecord::Base.extend(DbCharmer::MultiDbProxy::ClassMethods)
42
51
  ActiveRecord::Base.extend(DbCharmer::MultiDbProxy::MasterSlaveClassMethods)
43
- #ActiveRecord::Base.send(:include, DbCharmer::MultiDbProxy::InstanceMethods)
52
+ ActiveRecord::Base.send(:include, DbCharmer::MultiDbProxy::InstanceMethods)
44
53
 
45
- # Enable connection proxy for associations and scopes
46
- ActiveRecord::Associations::AssociationProxy.send(:include, DbCharmer::AssociationProxy::InstanceMethods)
54
+ # Enable connection proxy for scopes
47
55
  ActiveRecord::NamedScope::Scope.send(:include, DbCharmer::ScopeProxy::InstanceMethods)
48
56
 
57
+ # Enable connection proxy for associations
58
+ # WARNING: Inject methods to association class right here (they proxy include calls somewhere else, so include does not work)
59
+ module ActiveRecord
60
+ module Associations
61
+ class AssociationProxy
62
+ def proxy?
63
+ true
64
+ end
65
+
66
+ def on_db(con, proxy_target = nil, &block)
67
+ proxy_target ||= self
68
+ @reflection.klass.on_db(con, proxy_target, &block)
69
+ end
70
+
71
+ def on_slave(con = nil, &block)
72
+ @reflection.klass.on_slave(con, self, &block)
73
+ end
74
+
75
+ def on_master(&block)
76
+ @reflection.klass.on_master(self, &block)
77
+ end
78
+ end
79
+ end
80
+ end
81
+
49
82
  puts "Doing the magic..."
50
83
 
51
84
  require 'db_charmer/db_magic'
@@ -20,7 +20,6 @@ module DbCharmer
20
20
  end
21
21
 
22
22
  def switch_connection_to(conn, require_config_to_exist = true)
23
- # puts "DEBUG: Assigning connection proxy for #{self}"
24
23
  self.db_charmer_connection_proxy = coerce_to_connection_proxy(conn, require_config_to_exist)
25
24
  self.hijack_connection!
26
25
  end
@@ -5,7 +5,7 @@ module DbCharmer
5
5
  MASTER_METHODS = [ :update, :create, :delete, :destroy, :delete_all, :destroy_all, :update_all, :update_counters ]
6
6
 
7
7
  SLAVE_METHODS.each do |slave_method|
8
- class_eval <<-EOF
8
+ class_eval <<-EOF, __FILE__, __LINE__ + 1
9
9
  def #{slave_method}(*args, &block)
10
10
  first_level_on_slave do
11
11
  super(*args, &block)
@@ -15,7 +15,7 @@ module DbCharmer
15
15
  end
16
16
 
17
17
  MASTER_METHODS.each do |master_method|
18
- class_eval <<-EOF
18
+ class_eval <<-EOF, __FILE__, __LINE__ + 1
19
19
  def #{master_method}(*args, &block)
20
20
  on_master do
21
21
  super(*args, &block)
@@ -1,8 +1,8 @@
1
1
  module DbCharmer
2
2
  module MultiDbProxy
3
3
  class OnDbProxy < BlankSlate
4
- def initialize(model_class, slave)
5
- @model = model_class
4
+ def initialize(proxy_target, slave)
5
+ @proxy_target = proxy_target
6
6
  @slave = slave
7
7
  end
8
8
 
@@ -10,11 +10,11 @@ module DbCharmer
10
10
 
11
11
  def method_missing(meth, *args, &block)
12
12
  # Switch connection and proxy the method call
13
- @model.on_db(@slave) do |m|
13
+ @proxy_target.on_db(@slave) do |m|
14
14
  res = m.__send__(meth, *args, &block)
15
15
 
16
- # If result is a scope, return a new proxy for it, otherwise return the result itself
17
- (res.class == ActiveRecord::NamedScope::Scope) ? OnDbProxy.new(res, @slave) : res
16
+ # If result is a scope/association, return a new proxy for it, otherwise return the result itself
17
+ (res.proxy?) ? OnDbProxy.new(res, @slave) : res
18
18
  end
19
19
  end
20
20
  end
@@ -22,7 +22,7 @@ module DbCharmer
22
22
  module ClassMethods
23
23
  def on_db(con, proxy_target = nil)
24
24
  proxy_target ||= self
25
-
25
+
26
26
  # Chain call
27
27
  return OnDbProxy.new(proxy_target, con) unless block_given?
28
28
 
@@ -38,7 +38,14 @@ module DbCharmer
38
38
  end
39
39
  end
40
40
  end
41
-
41
+
42
+ module InstanceMethods
43
+ def on_db(con, proxy_target = nil, &block)
44
+ proxy_target ||= self
45
+ self.class.on_db(con, proxy_target, &block)
46
+ end
47
+ end
48
+
42
49
  module MasterSlaveClassMethods
43
50
  def on_slave(con = nil, proxy_target = nil, &block)
44
51
  con ||= db_charmer_random_slave
@@ -1,9 +1,13 @@
1
1
  module DbCharmer
2
2
  module ScopeProxy
3
3
  module InstanceMethods
4
+ def proxy?
5
+ true
6
+ end
7
+
4
8
  def on_db(con, proxy_target = nil, &block)
5
9
  proxy_target ||= self
6
- proxy_scope.on_db(con, self, &block)
10
+ proxy_scope.on_db(con, proxy_target, &block)
7
11
  end
8
12
 
9
13
  def on_slave(con = nil, &block)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: db-charmer
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.4
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexey Kovyrin
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-10-28 00:00:00 -04:00
12
+ date: 2009-11-02 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -42,7 +42,6 @@ files:
42
42
  - init.rb
43
43
  - lib/db_charmer.rb
44
44
  - lib/db_charmer/active_record_extensions.rb
45
- - lib/db_charmer/association_proxy.rb
46
45
  - lib/db_charmer/connection_factory.rb
47
46
  - lib/db_charmer/connection_proxy.rb
48
47
  - lib/db_charmer/connection_switch.rb
@@ -1,18 +0,0 @@
1
- module DbCharmer
2
- module AssociationProxy
3
- module InstanceMethods
4
- def on_db(con, proxy_target = nil, &block)
5
- proxy_target ||= self
6
- @reflection.klass.on_db(con, proxy_target, &block)
7
- end
8
-
9
- def on_slave(con = nil, &block)
10
- @reflection.klass.on_slave(con, self, &block)
11
- end
12
-
13
- def on_master(&block)
14
- @reflection.klass.on_master(self, &block)
15
- end
16
- end
17
- end
18
- end