Module: ActiveRecordShards::ConfigurationParser

Included in:
ActiveRecord::Base
Defined in:
lib/active_record_shards/configuration_parser.rb

Class Method Summary collapse

Class Method Details

.configurations_with_shard_explosion=(conf) ⇒ Object



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

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

.expand_child!(parent, child) ⇒ Object



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

def expand_child!(parent, child)
  parent.each do |key, value|
    unless ['slave', 'shards'].include?(key) || value.is_a?(Hash)
      child[key] ||= value
    end
  end
end

.explode(conf) ⇒ Object



8
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
# File 'lib/active_record_shards/configuration_parser.rb', line 8

def explode(conf)
  conf = conf.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 slave_conf = env_config.delete('slave')
      expand_child!(env_config, slave_conf)
      conf["#{env_name}_slave"] = slave_conf
    end
  end

  conf
end

.extended(base) ⇒ Object



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

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