Module: ActiveRecordShards::ConfigurationParser

Defined in:
lib/active_record_shards/configuration_parser.rb

Class Method Summary collapse

Class Method Details

.configurations_with_shard_explosion=(conf) ⇒ Object



44
45
46
# File 'lib/active_record_shards/configuration_parser.rb', line 44

def configurations_with_shard_explosion=(conf)
  self.configurations_without_shard_explosion = explode(conf)
end

.expand_child!(parent, child) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/active_record_shards/configuration_parser.rb', line 36

def expand_child!(parent, child)
  parent.each do |key, value|
    unless ['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
# 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
  end

  conf
end

.extended(base) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/active_record_shards/configuration_parser.rb', line 48

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