Module: Octopus::Model::ClassMethods

Includes:
SharedMethods
Defined in:
lib/octopus/model.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SharedMethods

#using

Class Method Details

.extended(base) ⇒ Object



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

def self.extended(base)
  base.class_attribute(:replicated)
  base.class_attribute(:sharded)
  base.class_attribute(:allowed_shards)
  base.hijack_methods
end

Instance Method Details

#allow_shard(*shards) ⇒ Object



105
106
107
108
# File 'lib/octopus/model.rb', line 105

def allow_shard(*shards)
  self.allowed_shards ||= []
  self.allowed_shards += shards
end

#allowed_shard?(shard) ⇒ Boolean

Returns:

  • (Boolean)


156
157
158
159
160
161
162
# File 'lib/octopus/model.rb', line 156

def allowed_shard?(shard)
  if custom_octopus_connection
    allowed_shards && shard && (allowed_shards.include?(shard.to_s) || allowed_shards.include?(shard.to_sym))
  else
    true
  end
end

#clear_active_connections_with_octopus!Object



181
182
183
184
185
186
187
# File 'lib/octopus/model.rb', line 181

def 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



189
190
191
192
193
194
195
# File 'lib/octopus/model.rb', line 189

def 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)


197
198
199
200
201
202
203
# File 'lib/octopus/model.rb', line 197

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

#connection_pool_with_octopusObject



173
174
175
176
177
178
179
# File 'lib/octopus/model.rb', line 173

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

#connection_proxyObject



142
143
144
145
146
# File 'lib/octopus/model.rb', line 142

def connection_proxy
  ActiveRecord::Base.class_variable_defined?(:@@connection_proxy) &&
    ActiveRecord::Base.class_variable_get(:@@connection_proxy) ||
    ActiveRecord::Base.class_variable_set(:@@connection_proxy, Octopus::Proxy.new)
end

#connection_with_octopusObject



164
165
166
167
168
169
170
171
# File 'lib/octopus/model.rb', line 164

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

#hijack_methodsObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/octopus/model.rb', line 110

def hijack_methods
  after_initialize :set_current_shard

  around_save :run_on_shard, :unless => lambda { self.class.custom_octopus_connection }

  class_attribute :custom_octopus_connection

  class << self
    attr_accessor :custom_octopus_table_name

    alias_method :connection_without_octopus, :connection
    alias_method :connection, :connection_with_octopus

    alias_method :connection_pool_without_octopus, :connection_pool
    alias_method :connection_pool, :connection_pool_with_octopus

    alias_method :clear_all_connections_without_octopus!, :clear_all_connections!
    alias_method :clear_all_connections!, :clear_all_connections_with_octopus!

    alias_method :clear_active_connections_without_octopus!, :clear_active_connections!
    alias_method :clear_active_connections!, :clear_active_connections_with_octopus!

    alias_method :connected_without_octopus?, :connected?
    alias_method :connected?, :connected_with_octopus?

    def table_name=(value = nil)
      self.custom_octopus_table_name = true
      super
    end
  end
end

#octopus_establish_connection(spec = ) ⇒ Object



210
211
212
213
# File 'lib/octopus/model.rb', line 210

def octopus_establish_connection(spec = ENV['DATABASE_URL'])
  self.custom_octopus_connection = true if spec
  establish_connection(spec)
end

#octopus_set_table_name(value = nil) ⇒ Object



215
216
217
218
# File 'lib/octopus/model.rb', line 215

def octopus_set_table_name(value = nil)
  ActiveSupport::Deprecation.warn 'Calling `octopus_set_table_name` is deprecated and will be removed in Octopus 1.0.', caller
  set_table_name(value)
end

#replicated_modelObject



97
98
99
# File 'lib/octopus/model.rb', line 97

def replicated_model
  self.replicated = true
end

#set_table_name_with_octopus(value = nil, &block) ⇒ Object



205
206
207
208
# File 'lib/octopus/model.rb', line 205

def set_table_name_with_octopus(value = nil, &block)
  self.custom_octopus_table_name = true
  set_table_name_without_octopus(value, &block)
end

#sharded_modelObject



101
102
103
# File 'lib/octopus/model.rb', line 101

def sharded_model
  self.sharded = true
end

#should_use_normal_connection?Boolean

Returns:

  • (Boolean)


148
149
150
151
152
153
154
# File 'lib/octopus/model.rb', line 148

def should_use_normal_connection?
  if !Octopus.enabled?
    true
  elsif custom_octopus_connection
    !connection_proxy.block || !allowed_shard?(connection_proxy.current_shard)
  end
end