class SwitchConnection::Config
Attributes
auto_master[RW]
auto_master?[RW]
env[RW]
Public Class Methods
new()
click to toggle source
# File lib/switch_connection/config.rb, line 8 def initialize self.auto_master = false self.env = :test end
Public Instance Methods
define_switch_point(name, config)
click to toggle source
# File lib/switch_connection/config.rb, line 13 def define_switch_point(name, config) assert_valid_config!(config) switch_points[name] = config end
each_key(&block)
click to toggle source
# File lib/switch_connection/config.rb, line 70 def each_key(&block) switch_points.each_key(&block) end
fetch(name)
click to toggle source
# File lib/switch_connection/config.rb, line 62 def fetch(name) switch_points.fetch(name) end
keys()
click to toggle source
# File lib/switch_connection/config.rb, line 66 def keys switch_points.keys end
master_database_name(name)
click to toggle source
# File lib/switch_connection/config.rb, line 22 def master_database_name(name) fetch(name)[:master] end
master_model_name(name)
click to toggle source
# File lib/switch_connection/config.rb, line 40 def master_model_name(name) "#{name.to_s.gsub(/\W+/, '_').camelize}_master".camelize if fetch(name)[:master] end
model_name(name, mode)
click to toggle source
# File lib/switch_connection/config.rb, line 30 def model_name(name, mode) if mode == :master master_model_name(name) else return unless slave_exist?(name) slave_mode_name(name, rand(slave_count(name))) end end
slave_count(name)
click to toggle source
# File lib/switch_connection/config.rb, line 52 def slave_count(name) return 0 unless slave_exist?(name) fetch(name)[:slaves].count end
slave_database_name(name, index)
click to toggle source
# File lib/switch_connection/config.rb, line 26 def slave_database_name(name, index) fetch(name)[:slaves][index] end
slave_exist?(name)
click to toggle source
# File lib/switch_connection/config.rb, line 48 def slave_exist?(name) fetch(name).key?(:slaves) end
slave_mode_name(name, index)
click to toggle source
# File lib/switch_connection/config.rb, line 44 def slave_mode_name(name, index) "#{name.to_s.gsub(/\W+/, '_').camelize}_slave_index_#{index}".camelize end
slave_mode_names(name)
click to toggle source
# File lib/switch_connection/config.rb, line 58 def slave_mode_names(name) (0..(fetch(name)[:slaves].count - 1)).map { |i| slave_mode_name(name, i) } end
switch_points()
click to toggle source
# File lib/switch_connection/config.rb, line 18 def switch_points @switch_points ||= {} end
Private Instance Methods
assert_valid_config!(config)
click to toggle source
# File lib/switch_connection/config.rb, line 76 def assert_valid_config!(config) unless config.key?(:master) || config.key?(:slaves) raise ArgumentError.new(':master or :slaves must be specified') end if config.key?(:slaves) unless config[:slaves].is_a?(Array) raise TypeError.new(":slaves's value must be Array") end end if config.key?(:master) unless config[:master].is_a?(Symbol) raise TypeError.new(":master's value must be ") end end nil end