Class: ActiveRecordShards::ShardSelection
- Inherits:
-
Object
- Object
- ActiveRecordShards::ShardSelection
- Defined in:
- lib/active_record_shards/shard_selection.rb
Constant Summary collapse
- NO_SHARD =
:_no_shard
- PRIMARY =
"primary".freeze
Instance Method Summary collapse
-
#initialize ⇒ ShardSelection
constructor
A new instance of ShardSelection.
- #on_slave=(new_slave) ⇒ Object
- #on_slave? ⇒ Boolean
- #options ⇒ Object
- #resolve_connection_name(sharded:, configurations:) ⇒ Object
- #shard ⇒ Object
- #shard=(new_shard) ⇒ Object
- #shard_name(klass = nil, try_slave = true) ⇒ Object
Constructor Details
#initialize ⇒ ShardSelection
Returns a new instance of ShardSelection.
7 8 9 10 |
# File 'lib/active_record_shards/shard_selection.rb', line 7 def initialize @on_slave = false @shard = nil end |
Instance Method Details
#on_slave=(new_slave) ⇒ Object
81 82 83 |
# File 'lib/active_record_shards/shard_selection.rb', line 81 def on_slave=(new_slave) @on_slave = (new_slave == true) end |
#on_slave? ⇒ Boolean
77 78 79 |
# File 'lib/active_record_shards/shard_selection.rb', line 77 def on_slave? @on_slave end |
#options ⇒ Object
85 86 87 |
# File 'lib/active_record_shards/shard_selection.rb', line 85 def { shard: @shard, slave: @on_slave } end |
#resolve_connection_name(sharded:, configurations:) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/active_record_shards/shard_selection.rb', line 50 def resolve_connection_name(sharded:, configurations:) resolved_shard = sharded ? shard : nil if !resolved_shard && !@on_slave return PRIMARY end @shard_names ||= {} @shard_names[ActiveRecordShards.rails_env] ||= {} @shard_names[ActiveRecordShards.rails_env][resolved_shard] ||= {} @shard_names[ActiveRecordShards.rails_env][resolved_shard][@on_slave] ||= begin s = ActiveRecordShards.rails_env.dup s << "_shard_#{resolved_shard}" if resolved_shard if @on_slave && configurations["#{s}_slave"] # fall back to master connection s << "_slave" end s end end |
#shard ⇒ Object
14 15 16 17 18 19 20 21 22 |
# File 'lib/active_record_shards/shard_selection.rb', line 14 def shard(klass = nil) if (@shard || self.class.default_shard) && (klass.nil? || klass.is_sharded?) if @shard == NO_SHARD nil else @shard || self.class.default_shard end end end |
#shard=(new_shard) ⇒ Object
73 74 75 |
# File 'lib/active_record_shards/shard_selection.rb', line 73 def shard=(new_shard) @shard = (new_shard || NO_SHARD) end |
#shard_name(klass = nil, try_slave = true) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/active_record_shards/shard_selection.rb', line 24 def shard_name(klass = nil, try_slave = true) the_shard = shard(klass) @shard_names ||= {} @shard_names[ActiveRecordShards.rails_env] ||= {} @shard_names[ActiveRecordShards.rails_env][the_shard] ||= {} @shard_names[ActiveRecordShards.rails_env][the_shard][try_slave] ||= {} @shard_names[ActiveRecordShards.rails_env][the_shard][try_slave][@on_slave] ||= begin s = ActiveRecordShards.rails_env.dup s << "_shard_#{the_shard}" if the_shard s << "_slave" if @on_slave && try_slave s end end |