Class: DbCharmer::Sharding::StubConnection
- Inherits:
-
Object
- Object
- DbCharmer::Sharding::StubConnection
show all
- Defined in:
- lib/db_charmer/sharding/stub_connection.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(sharded_connection) ⇒ StubConnection
Returns a new instance of StubConnection.
12
13
14
15
|
# File 'lib/db_charmer/sharding/stub_connection.rb', line 12
def initialize(sharded_connection)
@sharded_connection = sharded_connection
@real_conn = nil
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &block) ⇒ Object
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/db_charmer/sharding/stub_connection.rb', line 40
def method_missing(meth, *args, &block)
if ::ActiveRecord::ConnectionAdapters::DatabaseStatements.instance_methods.member?(meth.to_s)
raise ::ActiveRecord::ConnectionNotEstablished, "You have to switch connection on your model before using it!"
end
unless real_connection
raise ::ActiveRecord::ConnectionNotEstablished, "No real connection to proxy this method to!"
end
if real_connection.kind_of?(DbCharmer::Sharding::StubConnection)
raise ::ActiveRecord::ConnectionNotEstablished, "You have to switch connection on your model before using it!"
end
real_connection.__send__(meth, *args, &block)
end
|
Instance Attribute Details
#sharded_connection ⇒ Object
Returns the value of attribute sharded_connection.
10
11
12
|
# File 'lib/db_charmer/sharding/stub_connection.rb', line 10
def sharded_connection
@sharded_connection
end
|
Instance Method Details
#db_charmer_connection_name ⇒ Object
21
22
23
|
# File 'lib/db_charmer/sharding/stub_connection.rb', line 21
def db_charmer_connection_name
"StubConnection"
end
|
#real_connection ⇒ Object
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
# File 'lib/db_charmer/sharding/stub_connection.rb', line 25
def real_connection
return @real_conn if @real_conn
conn = sharded_connection.shard_connections.try(:first)
conn ||= sharded_connection.sharder.shard_for_key(:default) if sharded_connection.support_default_shard?
return nil unless conn
@real_conn = ::ActiveRecord::Base.coerce_to_connection_proxy(conn, DbCharmer.connections_should_exist?)
end
|
#set_real_connection(real_conn) ⇒ Object
17
18
19
|
# File 'lib/db_charmer/sharding/stub_connection.rb', line 17
def set_real_connection(real_conn)
@real_conn = real_conn
end
|