Module: Octopus::Model::SharedMethods

Included in:
ClassMethods, InstanceMethods
Defined in:
lib/octopus/model.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.clear_active_connections_with_octopus!Object



75
76
77
78
79
80
81
# File 'lib/octopus/model.rb', line 75

def self.clear_active_connections_with_octopus!
  if should_use_normal_connection?
    clear_active_connections_without_octopus!
  else
    connection_proxy.clear_active_connections!
  end
end

.clear_all_connections_with_octopus!Object



83
84
85
86
87
88
89
# File 'lib/octopus/model.rb', line 83

def self.clear_all_connections_with_octopus!
  if should_use_normal_connection?
    clear_all_connections_without_octopus!
  else
    connection_proxy.clear_all_connections!
  end
end

.connected_with_octopus?Boolean

Returns:

  • (Boolean)


91
92
93
94
95
96
97
# File 'lib/octopus/model.rb', line 91

def self.connected_with_octopus?
  if should_use_normal_connection?
    connected_without_octopus?
  else
    connection_proxy.connected?
  end
end

.connection_pool_with_octopusObject



67
68
69
70
71
72
73
# File 'lib/octopus/model.rb', line 67

def self.connection_pool_with_octopus
  if should_use_normal_connection?
    connection_pool_without_octopus
  else
    connection_proxy.connection_pool
  end
end

.connection_proxyObject



54
55
56
# File 'lib/octopus/model.rb', line 54

def self.connection_proxy
  @@connection_proxy ||= Octopus::Proxy.new
end

.connection_with_octopusObject



58
59
60
61
62
63
64
65
# File 'lib/octopus/model.rb', line 58

def self.connection_with_octopus
  if should_use_normal_connection?
    connection_without_octopus
  else
    self.connection_proxy.current_model = self
    self.connection_proxy
  end
end

.should_use_normal_connection?Boolean

Returns:

  • (Boolean)


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

def self.should_use_normal_connection?
  !Octopus.enabled? || self.custom_octopus_connection
end

Instance Method Details

#clean_table_nameObject



12
13
14
15
16
17
18
19
20
21
# File 'lib/octopus/model.rb', line 12

def clean_table_name
  return unless self.connection_proxy.should_clean_table_name?

  if self != ActiveRecord::Base && self.respond_to?(:reset_table_name) && !self.custom_octopus_table_name
    self.reset_table_name()
  end

  self.reset_column_information
  self.instance_variable_set(:@quoted_table_name, nil)
end

#hijack_connectionObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/octopus/model.rb', line 49

def hijack_connection()
  def self.should_use_normal_connection?
    !Octopus.enabled? || self.custom_octopus_connection
  end

  def self.connection_proxy
    @@connection_proxy ||= Octopus::Proxy.new
  end

  def self.connection_with_octopus
    if should_use_normal_connection?
      connection_without_octopus
    else
      self.connection_proxy.current_model = self
      self.connection_proxy
    end
  end

  def self.connection_pool_with_octopus
    if should_use_normal_connection?
      connection_pool_without_octopus
    else
      connection_proxy.connection_pool
    end
  end

  def self.clear_active_connections_with_octopus!
    if should_use_normal_connection?
      clear_active_connections_without_octopus!
    else
      connection_proxy.clear_active_connections!
    end
  end

  def self.clear_all_connections_with_octopus!
    if should_use_normal_connection?
      clear_all_connections_without_octopus!
    else
      connection_proxy.clear_all_connections!
    end
  end

  def self.connected_with_octopus?
    if should_use_normal_connection?
      connected_without_octopus?
    else
      connection_proxy.connected?
    end
  end

  class << self
    alias_method_chain :connection, :octopus
    alias_method_chain :connection_pool, :octopus
    alias_method_chain :clear_all_connections!, :octopus
    alias_method_chain :clear_active_connections!, :octopus
    alias_method_chain :connected?, :octopus
  end
end

#hijack_initializerObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/octopus/model.rb', line 32

def hijack_initializer()
  attr_accessor :current_shard
  around_save :run_on_shard

  def set_current_shard
    return unless Octopus.enabled?

    if new_record? || self.class.connection_proxy.block
      self.current_shard = self.class.connection_proxy.current_shard
    else
      self.current_shard = self.class.connection_proxy.last_current_shard || self.class.connection_proxy.current_shard
    end
  end

  after_initialize :set_current_shard
end

#set_current_shardObject



36
37
38
39
40
41
42
43
44
# File 'lib/octopus/model.rb', line 36

def set_current_shard
  return unless Octopus.enabled?

  if new_record? || self.class.connection_proxy.block
    self.current_shard = self.class.connection_proxy.current_shard
  else
    self.current_shard = self.class.connection_proxy.last_current_shard || self.class.connection_proxy.current_shard
  end
end

#using(shard) ⇒ Object



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

def using(shard)
  if Octopus.enabled?
    clean_table_name
    Octopus::ScopeProxy.new(shard, self)
  else
    self
  end
end