Class: ActiveRecord::Turntable::Shard

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/turntable/shard.rb

Direct Known Subclasses

MasterShard, SeqShard

Defined Under Namespace

Modules: Connections

Constant Summary collapse

DEFAULT_CONFIG =
{
  "connection" => (defined?(Rails) ? Rails.env : "development")
}.with_indifferent_access

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(shard_spec) ⇒ Shard

Returns a new instance of Shard.



11
12
13
14
15
# File 'lib/active_record/turntable/shard.rb', line 11

def initialize(shard_spec)
  @config = DEFAULT_CONFIG.merge(shard_spec)
  @name = @config["connection"]
  ActiveRecord::Base.turntable_connections[name] = connection_pool
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/active_record/turntable/shard.rb', line 9

def name
  @name
end

Instance Method Details

#connectionObject



21
22
23
24
25
# File 'lib/active_record/turntable/shard.rb', line 21

def connection
  connection_pool.connection.tap do |conn|
    conn.turntable_shard_name ||= name
  end
end

#connection_poolObject



17
18
19
# File 'lib/active_record/turntable/shard.rb', line 17

def connection_pool
  connection_klass.connection_pool
end