Module: ActiveRecordShards::ConfigurationParser
- Included in:
- ActiveRecord::Base
- Defined in:
- lib/active_record_shards/configuration_parser.rb
Class Method Summary collapse
- .configurations_with_shard_explosion=(conf) ⇒ Object
- .expand_child!(parent, child) ⇒ Object
- .explode(conf) ⇒ Object
- .extended(base) ⇒ Object
- .replace_slave_keys(conf) ⇒ Object
Class Method Details
.configurations_with_shard_explosion=(conf) ⇒ Object
65 66 67 68 69 |
# File 'lib/active_record_shards/configuration_parser.rb', line 65 def configurations_with_shard_explosion=(conf) exploded_configuration = explode(conf) configuration_with_slave_keys_replaced = replace_slave_keys(exploded_configuration) self.configurations_without_shard_explosion = configuration_with_slave_keys_replaced end |
.expand_child!(parent, child) ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/active_record_shards/configuration_parser.rb', line 57 def (parent, child) parent.each do |key, value| unless ['slave', 'replica', 'shards'].include?(key) || value.is_a?(Hash) child[key] ||= value end end end |
.explode(conf) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/active_record_shards/configuration_parser.rb', line 9 def explode(conf) conf = conf.to_h.deep_dup conf.to_a.each do |env_name, env_config| next unless shards = env_config.delete('shards') unless shards.keys.all? { |shard_name| shard_name.is_a?(Integer) } raise "All shard names must be integers: #{shards.keys.inspect}." end env_config['shard_names'] = shards.keys shards.each do |shard_name, shard_conf| (env_config, shard_conf) conf["#{env_name}_shard_#{shard_name}"] = shard_conf end end conf.to_a.each do |env_name, env_config| if replica_conf = env_config.delete('replica') (env_config, replica_conf) conf["#{env_name}_replica"] = replica_conf end # rubocop:disable Style/Next if legacy_replica_conf = env_config.delete('slave') ActiveRecordShards::Deprecation.warn('`slave` configuration keys should be replaced with `replica` keys!') (env_config, legacy_replica_conf) conf["#{env_name}_replica"] = legacy_replica_conf end # rubocop:enable Style/Next end conf end |
.extended(base) ⇒ Object
71 72 73 74 75 76 77 |
# File 'lib/active_record_shards/configuration_parser.rb', line 71 def self.extended(base) base.singleton_class.send(:alias_method, :configurations_without_shard_explosion=, :configurations=) base.singleton_class.send(:alias_method, :configurations=, :configurations_with_shard_explosion=) base.singleton_class.send(:public, :configurations=) base.configurations = base.configurations if base.configurations.present? end |
.replace_slave_keys(conf) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/active_record_shards/configuration_parser.rb', line 44 def replace_slave_keys(conf) conf.to_a.each do |env_name, env_config| next unless env_name.end_with?("_slave") replica_key = env_name.sub(/_slave$/, "_replica") next if conf.key?(replica_key) conf[replica_key] = env_config.deep_dup end conf end |