Module: SwitchConnection::Model::ClassMethods

Defined in:
lib/switch_connection/model.rb

Instance Method Summary collapse

Instance Method Details

#switch_point_nameObject



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

def switch_point_name
  thread_local_switch_point_name || @global_switch_point_name
end

#switch_point_proxyObject



76
77
78
79
80
81
82
83
84
# File 'lib/switch_connection/model.rb', line 76

def switch_point_proxy
  if switch_point_name
    ProxyRepository.checkout(switch_point_name)
  elsif self == ActiveRecord::Base
    nil
  else
    superclass.switch_point_proxy
  end
end

#thread_local_switch_point_nameObject



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

def thread_local_switch_point_name
  Thread.current[:"thread_local_#{name}_switch_point_name"]
end

#transaction_with(*models, &block) ⇒ Object



86
87
88
89
90
91
92
93
94
# File 'lib/switch_connection/model.rb', line 86

def transaction_with(*models, &block)
  unless can_transaction_with?(*models)
    raise Error.new("switch_point's model names must be consistent")
  end

  with_master do
    transaction(&block)
  end
end

#use_switch_point(name) ⇒ Object



49
50
51
52
# File 'lib/switch_connection/model.rb', line 49

def use_switch_point(name)
  assert_existing_switch_point!(name)
  @global_switch_point_name = name
end

#with_master(&block) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/switch_connection/model.rb', line 41

def with_master(&block)
  if switch_point_proxy
    switch_point_proxy.with_master(&block)
  else
    raise UnconfiguredError.new("#{name} isn't configured to use switch_point")
  end
end

#with_slave(&block) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/switch_connection/model.rb', line 33

def with_slave(&block)
  if switch_point_proxy
    switch_point_proxy.with_slave(&block)
  else
    raise UnconfiguredError.new("#{name} isn't configured to use switch_point")
  end
end

#with_switch_point(new_switch_point_name) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/switch_connection/model.rb', line 54

def with_switch_point(new_switch_point_name)
  saved_switch_point_name = thread_local_switch_point_name
  self.thread_local_switch_point_name = new_switch_point_name
  yield
ensure
  self.thread_local_switch_point_name = saved_switch_point_name
end