Class: SwitchConnection::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/switch_connection/config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



8
9
10
11
# File 'lib/switch_connection/config.rb', line 8

def initialize
  self.auto_master = false
  self.env = :test
end

Instance Attribute Details

#auto_masterObject Also known as: auto_master?

Returns the value of attribute auto_master.



5
6
7
# File 'lib/switch_connection/config.rb', line 5

def auto_master
  @auto_master
end

#envObject

Returns the value of attribute env.



5
6
7
# File 'lib/switch_connection/config.rb', line 5

def env
  @env
end

Instance Method Details

#define_switch_point(name, config) ⇒ Object



13
14
15
16
# 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) ⇒ Object



70
71
72
# File 'lib/switch_connection/config.rb', line 70

def each_key(&block)
  switch_points.each_key(&block)
end

#fetch(name) ⇒ Object



62
63
64
# File 'lib/switch_connection/config.rb', line 62

def fetch(name)
  switch_points.fetch(name)
end

#keysObject



66
67
68
# File 'lib/switch_connection/config.rb', line 66

def keys
  switch_points.keys
end

#master_database_name(name) ⇒ Object



22
23
24
# File 'lib/switch_connection/config.rb', line 22

def master_database_name(name)
  fetch(name)[:master]
end

#master_model_name(name) ⇒ Object



40
41
42
# 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) ⇒ Object



30
31
32
33
34
35
36
37
38
# 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) ⇒ Object



52
53
54
55
56
# 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) ⇒ Object



26
27
28
# File 'lib/switch_connection/config.rb', line 26

def slave_database_name(name, index)
  fetch(name)[:slaves][index]
end

#slave_exist?(name) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/switch_connection/config.rb', line 48

def slave_exist?(name)
  fetch(name).key?(:slaves)
end

#slave_mode_name(name, index) ⇒ Object



44
45
46
# 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) ⇒ Object



58
59
60
# 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_pointsObject



18
19
20
# File 'lib/switch_connection/config.rb', line 18

def switch_points
  @switch_points ||= {}
end