Module: DbCharmer::ActiveRecord::Sharding
- Defined in:
- lib/db_charmer/active_record/sharding.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#on_default_shard(proxy_target = nil, &block) ⇒ Object
Run on default shard (if supported by the sharding method).
-
#on_each_shard(proxy_target = nil, &block) ⇒ Object
Enumerate shards.
- #shard_for(key, proxy_target = nil, &block) ⇒ Object
Class Method Details
.extended(model) ⇒ Object
5 6 7 |
# File 'lib/db_charmer/active_record/sharding.rb', line 5 def self.extended(model) model.cattr_accessor(:sharded_connection) end |
Instance Method Details
#on_default_shard(proxy_target = nil, &block) ⇒ Object
Run on default shard (if supported by the sharding method)
16 17 18 19 20 21 22 23 24 |
# File 'lib/db_charmer/active_record/sharding.rb', line 16 def on_default_shard(proxy_target = nil, &block) raise ArgumentError, "No sharded connection configured!" unless sharded_connection if sharded_connection.support_default_shard? shard_for(:default, proxy_target, &block) else raise ArgumentError, "This model's sharding method does not support default shard" end end |
#on_each_shard(proxy_target = nil, &block) ⇒ Object
Enumerate shards
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/db_charmer/active_record/sharding.rb', line 27 def on_each_shard(proxy_target = nil, &block) raise ArgumentError, "No sharded connection configured!" unless sharded_connection conns = sharded_connection.shard_connections raise ArgumentError, "This model's sharding method does not support shards enumeration" unless conns conns.each do |conn| on_db(conn, proxy_target, &block) end end |
#shard_for(key, proxy_target = nil, &block) ⇒ Object
9 10 11 12 13 |
# File 'lib/db_charmer/active_record/sharding.rb', line 9 def shard_for(key, proxy_target = nil, &block) raise ArgumentError, "No sharded connection configured!" unless sharded_connection conn = sharded_connection.sharder.shard_for_key(key) on_db(conn, proxy_target, &block) end |