Class: Octopus::ScopeProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/octopus/scope_proxy.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(shard, klass) ⇒ ScopeProxy

Returns a new instance of ScopeProxy.



4
5
6
7
# File 'lib/octopus/scope_proxy.rb', line 4

def initialize(shard, klass)
  @shard = shard
  @klass = klass
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



36
37
38
39
40
41
# File 'lib/octopus/scope_proxy.rb', line 36

def method_missing(method, *args, &block)
  @klass.connection().current_shard = @shard
  @klass = @klass.send(method, *args, &block)
  return @klass if @klass.is_a?(ActiveRecord::Base) or @klass.is_a?(Array) or @klass.is_a?(Fixnum) or @klass.nil?
  return self
end

Instance Attribute Details

#klassObject

Returns the value of attribute klass.



2
3
4
# File 'lib/octopus/scope_proxy.rb', line 2

def klass
  @klass
end

#shardObject

Returns the value of attribute shard.



2
3
4
# File 'lib/octopus/scope_proxy.rb', line 2

def shard
  @shard
end

Instance Method Details

#==(other) ⇒ Object



43
44
45
46
# File 'lib/octopus/scope_proxy.rb', line 43

def ==(other)
  @shard == other.shard
  @klass == other.klass
end

#connectionObject



31
32
33
34
# File 'lib/octopus/scope_proxy.rb', line 31

def connection
  @klass.connection().current_shard = @shard
  @klass.connection()
end

#transaction(options = {}, &block) ⇒ Object

Transaction Method send all queries to a specified shard.



20
21
22
23
24
25
26
27
28
29
# File 'lib/octopus/scope_proxy.rb', line 20

def transaction(options = {}, &block)
  @klass.connection().current_shard = @shard
  @klass.connection().block = true
  
  begin
    @klass.connection().transaction(options, &block)
  ensure
    @klass.connection().block = false    
  end
end

#using(shard, &block) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/octopus/scope_proxy.rb', line 9

def using(shard, &block)
  @shard = shard
  
  if block_given?
    @klass.connection.run_queries_on_shard(@shard, &block)
  end
  
  return self
end