Class: ActiveRecord::Turntable::Shard
- Inherits:
-
Object
- Object
- ActiveRecord::Turntable::Shard
show all
- Defined in:
- lib/active_record/turntable/shard.rb
Defined Under Namespace
Modules: Connections
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(cluster, name = defined?(Rails) ? Rails.env : "development", slaves = []) ⇒ Shard
Returns a new instance of Shard.
10
11
12
13
14
|
# File 'lib/active_record/turntable/shard.rb', line 10
def initialize(cluster, name = defined?(Rails) ? Rails.env : "development", slaves = [])
@cluster = cluster
@name = name
@slaves = slaves.map { |s| SlaveShard.new(cluster, s) }
end
|
Instance Attribute Details
#cluster ⇒ Object
Returns the value of attribute cluster.
8
9
10
|
# File 'lib/active_record/turntable/shard.rb', line 8
def cluster
@cluster
end
|
#name ⇒ Object
Returns the value of attribute name.
8
9
10
|
# File 'lib/active_record/turntable/shard.rb', line 8
def name
@name
end
|
#slaves ⇒ Object
Returns the value of attribute slaves.
8
9
10
|
# File 'lib/active_record/turntable/shard.rb', line 8
def slaves
@slaves
end
|
Class Method Details
.connection_classes ⇒ Object
4
5
6
|
# File 'lib/active_record/turntable/shard.rb', line 4
def self.connection_classes
Connections.constants.map { |name| Connections.const_get(name) }
end
|
Instance Method Details
#connection ⇒ Object
20
21
22
23
24
25
26
27
28
|
# File 'lib/active_record/turntable/shard.rb', line 20
def connection
if use_slave?
current_slave_shard.connection
else
connection_pool.connection.tap do |conn|
conn.turntable_shard_name ||= name
end
end
end
|
#connection_pool ⇒ Object
16
17
18
|
# File 'lib/active_record/turntable/shard.rb', line 16
def connection_pool
connection_klass.connection_pool
end
|
#current_slave_shard ⇒ Object
#support_slave? ⇒ Boolean
30
31
32
|
# File 'lib/active_record/turntable/shard.rb', line 30
def support_slave?
@slaves.size > 0
end
|
#use_slave? ⇒ Boolean
34
35
36
|
# File 'lib/active_record/turntable/shard.rb', line 34
def use_slave?
support_slave? && cluster.slave_enabled?
end
|