Module: SwitchPoint::Model::ClassMethods

Defined in:
lib/switch_point/model.rb

Instance Method Summary collapse

Instance Method Details

#cache_with_switch_point(&block) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/switch_point/model.rb', line 23

def cache_with_switch_point(&block)
  if switch_point_proxy
    switch_point_proxy.cache(&block)
  else
    cache_without_switch_point(&block)
  end
end

#connection_with_switch_pointObject



15
16
17
18
19
20
21
# File 'lib/switch_point/model.rb', line 15

def connection_with_switch_point
  if switch_point_proxy
    switch_point_proxy.connection
  else
    connection_without_switch_point
  end
end

#switch_point_proxyObject



60
61
62
63
64
65
66
67
68
# File 'lib/switch_point/model.rb', line 60

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

#transaction_with(*models, &block) ⇒ Object



70
71
72
73
74
75
76
77
78
# File 'lib/switch_point/model.rb', line 70

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

  with_writable do
    self.transaction(&block)
  end
end

#uncached_with_switch_point(&block) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/switch_point/model.rb', line 31

def uncached_with_switch_point(&block)
  if switch_point_proxy
    switch_point_proxy.uncached(&block)
  else
    uncached_without_switch_point(&block)
  end
end

#use_switch_point(name) ⇒ Object



55
56
57
58
# File 'lib/switch_point/model.rb', line 55

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

#with_readonly(&block) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/switch_point/model.rb', line 39

def with_readonly(&block)
  if switch_point_proxy
    switch_point_proxy.with_readonly(&block)
  else
    block.call
  end
end

#with_writable(&block) ⇒ Object



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

def with_writable(&block)
  if switch_point_proxy
    switch_point_proxy.with_writable(&block)
  else
    block.call
  end
end