module ActiveRecordShards::ConfigurationParser
Public Class Methods
extended(base)
click to toggle source
# File lib/active_record_shards/configuration_parser.rb, line 56 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
Public Instance Methods
configurations_with_shard_explosion=(conf)
click to toggle source
# File lib/active_record_shards/configuration_parser.rb, line 52 def configurations_with_shard_explosion=(conf) self.configurations_without_shard_explosion = explode(conf) end
expand_child!(parent, child)
click to toggle source
# File lib/active_record_shards/configuration_parser.rb, line 44 def expand_child!(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)
click to toggle source
# 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| expand_child!(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') expand_child!(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!') expand_child!(env_config, legacy_replica_conf) conf["#{env_name}_replica"] = legacy_replica_conf end # rubocop:enable Style/Next end conf end