Skip to content
This repository
Browse code

Instace/Class methods

  • Loading branch information...
commit e8f4e4e47605f9fe7066f69bd22368fb725ca68d 1 parent 4ff6f5d
Thiago Pradi authored

Showing 1 changed file with 33 additions and 26 deletions. Show diff stats Hide diff stats

  1. +33 26 lib/octopus/model.rb
59 lib/octopus/model.rb
... ... @@ -1,10 +1,30 @@
1 1 module Octopus::Model
2 2 def self.extended(base)
3 3 base.send(:include, InstanceMethods)
  4 + base.extend(ClassMethods)
4 5 base.hijack_connection()
5 6 end
6 7
7   - module InstanceMethods
  8 + module SharedMethods
  9 + def clean_table_name
  10 + self.reset_table_name() if self != ActiveRecord::Base && self.respond_to?(:reset_table_name)
  11 + end
  12 +
  13 + def using(shard, &block)
  14 + hijack_connection()
  15 + clean_table_name()
  16 +
  17 + if block_given?
  18 + self.connection.run_queries_on_shard(shard, &block)
  19 + else
  20 + hijack_initializer()
  21 + self.connection.current_shard = shard
  22 + self.connection.using_enabled = true
  23 +
  24 + return self
  25 + end
  26 + end
  27 +
8 28 def hijack_initializer()
9 29 attr_accessor :current_shard
10 30 after_initialize :set_current_shard
@@ -32,26 +52,11 @@ def connection
32 52 end
33 53 end
34 54 end
  55 + end
35 56
36   - def using(shard, &block)
37   - hijack_connection()
38   - clean_table_name()
39   -
40   - if block_given?
41   - self.connection_proxy.run_queries_on_shard(shard, &block)
42   - else
43   - hijack_initializer()
44   - self.connection_proxy.current_shard = shard
45   - self.connection_proxy.using_enabled = true
46   -
47   - return self
48   - end
49   - end
50   -
51   - def have_a_valid_shard?
52   - self.respond_to?(:current_shard) && self.current_shard != nil
53   - end
54   -
  57 + module InstanceMethods
  58 + include SharedMethods
  59 +
55 60 def set_connection(*args)
56 61 if(args.size == 1)
57 62 arg = args.first
@@ -60,16 +65,18 @@ def set_connection(*args)
60 65
61 66 self.class.connection_proxy.current_shard = self.current_shard if have_a_valid_shard?
62 67 end
63   -
64   - def clean_table_name
65   - self.reset_table_name() if self != ActiveRecord::Base && self.respond_to?(:reset_table_name)
  68 +
  69 + def have_a_valid_shard?
  70 + self.respond_to?(:current_shard) && self.current_shard != nil
66 71 end
67 72 end
68 73
69   - include InstanceMethods
  74 + module ClassMethods
  75 + include SharedMethods
70 76
71   - def replicated_model()
72   - write_inheritable_attribute(:replicated, true)
  77 + def replicated_model()
  78 + write_inheritable_attribute(:replicated, true)
  79 + end
73 80 end
74 81 end
75 82

0 comments on commit e8f4e4e

Please sign in to comment.
Something went wrong with that request. Please try again.