Module: Rails::Sharding::ShardableModel::ClassMethodsOverrides

Defined in:
lib/rails/sharding/shardable_model.rb

Overview

Module that includes all class methods that will be overriden on the model class when Rails::Sharding::ShardableModel is included

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(klass) ⇒ Object

dinamically saves original methods with prefix “original_” and overrides then with the methods with prefix “sharded_”



22
23
24
25
26
27
28
29
30
31
# File 'lib/rails/sharding/shardable_model.rb', line 22

def self.extended(klass)
  self.instance_methods.each do |sharded_method_name|
    method_name = sharded_method_name.to_s.match(/sharded_(.+)/)[1].to_sym

    klass.singleton_class.instance_eval do
      alias_method "original_#{method_name}".to_sym, method_name
      alias_method method_name, sharded_method_name
    end
  end
end

Instance Method Details

#sharded_clear_active_connections!Object



85
86
87
88
89
90
91
# File 'lib/rails/sharding/shardable_model.rb', line 85

def sharded_clear_active_connections!
  if ShardThreadRegistry.connecting_to_master?
    return original_clear_active_connections!
  else
    return ConnectionHandler.connection_handler.clear_active_connections!
  end
end

#sharded_clear_all_connections!Object



103
104
105
106
107
108
109
# File 'lib/rails/sharding/shardable_model.rb', line 103

def sharded_clear_all_connections!
  if ShardThreadRegistry.connecting_to_master?
    return original_clear_all_connections!
  else
    return ConnectionHandler.connection_handler.clear_all_connections!
  end
end

#sharded_clear_reloadable_connections!Object



94
95
96
97
98
99
100
# File 'lib/rails/sharding/shardable_model.rb', line 94

def sharded_clear_reloadable_connections!
  if ShardThreadRegistry.connecting_to_master?
    return original_clear_reloadable_connections!
  else
    return ConnectionHandler.connection_handler.clear_reloadable_connections!
  end
end

#sharded_connected?Boolean

Returns:

  • (Boolean)


56
57
58
59
60
61
62
# File 'lib/rails/sharding/shardable_model.rb', line 56

def sharded_connected?
  if ShardThreadRegistry.connecting_to_master?
    return original_connected?
  else
    return ConnectionHandler.connected?(*ShardThreadRegistry.current_shard_group_and_name)
  end
end

#sharded_connection_poolObject



34
35
36
37
38
39
40
41
42
# File 'lib/rails/sharding/shardable_model.rb', line 34

def sharded_connection_pool
  ShardThreadRegistry.notify_connection_retrieved

  if ShardThreadRegistry.connecting_to_master?
    return original_connection_pool
  else
    return ConnectionHandler.connection_pool(*ShardThreadRegistry.current_shard_group_and_name)
  end
end

#sharded_establish_connection(spec = nil) ⇒ Object

In the case we are connecting to a shard, ignore spec parameter and use what is in ShardThreadRegistry instead



76
77
78
79
80
81
82
# File 'lib/rails/sharding/shardable_model.rb', line 76

def sharded_establish_connection(spec=nil)
  if ShardThreadRegistry.connecting_to_master?
    return original_establish_connection(spec)
  else
    return ConnectionHandler.establish_connection(*ShardThreadRegistry.current_shard_group_and_name)
  end
end

#sharded_remove_connection(klass = nil) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/rails/sharding/shardable_model.rb', line 65

def sharded_remove_connection(klass=nil)
  if ShardThreadRegistry.connecting_to_master? || klass
    return klass ? original_remove_connection(klass) : original_remove_connection
  else
    return ConnectionHandler.remove_connection(*ShardThreadRegistry.current_shard_group_and_name)
  end
end

#sharded_retrieve_connectionObject



45
46
47
48
49
50
51
52
53
# File 'lib/rails/sharding/shardable_model.rb', line 45

def sharded_retrieve_connection
  ShardThreadRegistry.notify_connection_retrieved

  if ShardThreadRegistry.connecting_to_master?
    return original_retrieve_connection
  else
    return ConnectionHandler.retrieve_connection(*ShardThreadRegistry.current_shard_group_and_name)
  end
end