Class: ActiveRecord::ShardFor::ClusterConfig
- Inherits:
-
Object
- Object
- ActiveRecord::ShardFor::ClusterConfig
- Defined in:
- lib/activerecord/shard_for/cluster_config.rb
Instance Attribute Summary collapse
-
#connection_registry ⇒ Object
readonly
Returns the value of attribute connection_registry.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#connections ⇒ Array<Symbol>
An array of connection name.
-
#fetch(key) ⇒ Symbol
Registered connection name.
-
#initialize(name) ⇒ ClusterConfig
constructor
A new instance of ClusterConfig.
- #register(key, connection_name) ⇒ Object
Constructor Details
#initialize(name) ⇒ ClusterConfig
Returns a new instance of ClusterConfig.
7 8 9 10 |
# File 'lib/activerecord/shard_for/cluster_config.rb', line 7 def initialize(name) @name = name @connection_registry = {} end |
Instance Attribute Details
#connection_registry ⇒ Object (readonly)
Returns the value of attribute connection_registry.
4 5 6 |
# File 'lib/activerecord/shard_for/cluster_config.rb', line 4 def connection_registry @connection_registry end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
4 5 6 |
# File 'lib/activerecord/shard_for/cluster_config.rb', line 4 def name @name end |
Instance Method Details
#connections ⇒ Array<Symbol>
Returns An array of connection name.
23 24 25 |
# File 'lib/activerecord/shard_for/cluster_config.rb', line 23 def connections connection_registry.values end |
#fetch(key) ⇒ Symbol
Returns registered connection name.
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/activerecord/shard_for/cluster_config.rb', line 30 def fetch(key) connection_registry.each do |connection_key, connection| case connection_key when Range then return connection if connection_key.include?(key) else return connection if connection_key == key end end raise KeyError.new, "#{key} is not registerd connection" end |
#register(key, connection_name) ⇒ Object
15 16 17 18 19 20 |
# File 'lib/activerecord/shard_for/cluster_config.rb', line 15 def register(key, connection_name) raise RuntimeError.new, "#{key} is registered" if connection_registry.key?(key) establish_connection(connection_name) connection_registry[key] = connection_name end |