Class: Octopus::ScopeProxy

Inherits:
Object
  • Object
show all
Includes:
Octopus::ShardTracking::Attribute
Defined in:
lib/octopus/scope_proxy.rb

Instance Attribute Summary collapse

Attributes included from Octopus::ShardTracking::Attribute

#current_shard

Instance Method Summary collapse

Methods included from Octopus::ShardTracking::Attribute

included, #set_current_shard

Constructor Details

#initialize(shard, klass) ⇒ ScopeProxy

Returns a new instance of ScopeProxy.



6
7
8
9
# File 'lib/octopus/scope_proxy.rb', line 6

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



27
28
29
30
31
32
33
34
35
36
# File 'lib/octopus/scope_proxy.rb', line 27

def method_missing(method, *args, &block)
  result = run_on_shard { @klass.send(method, *args, &block) }

  if result.respond_to?(:all)
    @klass = result
    return self
  end

  result
end

Instance Attribute Details

#klassObject

Returns the value of attribute klass.



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

def klass
  @klass
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?

Delegates to method_missing (instead of @klass) so that User.using(:blah).where(:name => “Mike”) gets run in the correct shard context when #== is evaluated.



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

def ==(other)
  method_missing(:==, other)
end

#as_json(options = nil) ⇒ Object



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

def as_json(options = nil)
  method_missing(:as_json, options)
end

#connectionObject



22
23
24
25
# File 'lib/octopus/scope_proxy.rb', line 22

def connection
  @klass.connection.current_shard = @current_shard
  @klass.connection
end

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

Transaction Method send all queries to a specified shard.



18
19
20
# File 'lib/octopus/scope_proxy.rb', line 18

def transaction(options = {}, &block)
  run_on_shard { @klass = klass.transaction(options, &block) }
end

#using(shard) ⇒ Object



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

def using(shard)
  fail "Nonexistent Shard Name: #{shard}" if @klass.connection.instance_variable_get(:@shards)[shard].nil?
  @current_shard = shard
  self
end