Module: Octopus::Model::SharedMethods
- Included in:
- ClassMethods, InstanceMethods
- Defined in:
- lib/octopus/model.rb
Class Method Summary collapse
- .clear_active_connections_with_octopus! ⇒ Object
- .clear_all_connections_with_octopus! ⇒ Object
- .connected_with_octopus? ⇒ Boolean
- .connection_pool_with_octopus ⇒ Object
- .connection_proxy ⇒ Object
- .connection_with_octopus ⇒ Object
- .should_use_normal_connection? ⇒ Boolean
Instance Method Summary collapse
- #clean_table_name ⇒ Object
- #hijack_connection ⇒ Object
- #hijack_initializer ⇒ Object
- #set_current_shard ⇒ Object
- #using(shard) ⇒ Object
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
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_octopus ⇒ Object
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_proxy ⇒ Object
54 55 56 |
# File 'lib/octopus/model.rb', line 54 def self.connection_proxy @@connection_proxy ||= Octopus::Proxy.new end |
.connection_with_octopus ⇒ Object
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 |
Instance Method Details
#clean_table_name ⇒ Object
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_connection ⇒ Object
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_initializer ⇒ Object
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_shard ⇒ Object
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 |