Class: DataFabric::ConnectionProxy
- Inherits:
-
Object
- Object
- DataFabric::ConnectionProxy
show all
- Defined in:
- lib/data_fabric/connection_proxy.rb
Instance Method Summary
collapse
Constructor Details
#initialize(model_class, options) ⇒ ConnectionProxy
Returns a new instance of ConnectionProxy.
65
66
67
68
69
70
71
72
73
|
# File 'lib/data_fabric/connection_proxy.rb', line 65
def initialize(model_class, options)
@model_class = model_class
@replicated = options[:replicated]
@shard_group = options[:shard_by]
@prefix = options[:prefix]
set_role('slave') if @replicated
@model_class.send :include, ActiveRecordConnectionMethods if @replicated
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
92
93
94
95
|
# File 'lib/data_fabric/connection_proxy.rb', line 92
def method_missing(method, *args, &block)
DataFabric.logger.debug { "Calling #{method} on #{connection}" }
connection.send(method, *args, &block)
end
|
Instance Method Details
#connected? ⇒ Boolean
110
111
112
|
# File 'lib/data_fabric/connection_proxy.rb', line 110
def connected?
current_pool.connected?
end
|
#connection ⇒ Object
114
115
116
|
# File 'lib/data_fabric/connection_proxy.rb', line 114
def connection
current_pool.connection
end
|
#connection_name ⇒ Object
97
98
99
|
# File 'lib/data_fabric/connection_proxy.rb', line 97
def connection_name
connection_name_builder.join('_')
end
|
#current_pool ⇒ Object
118
119
120
121
122
123
124
125
|
# File 'lib/data_fabric/connection_proxy.rb', line 118
def current_pool
name = connection_name
self.class.shard_pools[name] ||= begin
config = ActiveRecord::Base.configurations[name]
raise ArgumentError, "Unknown database config: #{name}, have #{ActiveRecord::Base.configurations.inspect}" unless config
ActiveRecord::ConnectionAdapters::ConnectionPool.new(spec_for(config))
end
end
|
#transaction(start_db_transaction = true, &block) ⇒ Object
81
82
83
84
85
86
87
88
89
90
|
# File 'lib/data_fabric/connection_proxy.rb', line 81
def transaction(start_db_transaction = true, &block)
return yield if in_transaction?
with_master do
connection.transaction(start_db_transaction, &block)
end
end
|
#with_master ⇒ Object
101
102
103
104
105
106
107
108
|
# File 'lib/data_fabric/connection_proxy.rb', line 101
def with_master
old_role = current_role
set_role('master')
yield
ensure
set_role(old_role)
end
|