Class: Rails::Sharding::ActiveRecordExtensions::ScopeProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/rails/sharding/active_record_extensions.rb

Overview

Return value of the #using_shard scope method. Allows us to chain the shard choice with other ActiveRecord scopes

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(shard_group, shard_name, original_scope) ⇒ ScopeProxy

Returns a new instance of ScopeProxy.



44
45
46
47
48
# File 'lib/rails/sharding/active_record_extensions.rb', line 44

def initialize(shard_group, shard_name, original_scope)
  @shard_group = shard_group
  @shard_name = shard_name
  @original_scope = original_scope
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rails/sharding/active_record_extensions.rb', line 57

def method_missing(method, *args, &block)
  # runs any method chained in the correct shard
  result = Core.using_shard(@shard_group, @shard_name) do
    @original_scope.send(method, *args, &block)
  end

  # if result is still a scope (responds to to_sql), update original scope
  # and return proxy to continue chaining
  if result.respond_to?(:to_sql)
    @original_scope = result
    return self
  end

  result
end

Instance Attribute Details

#original_scopeObject

Returns the value of attribute original_scope.



42
43
44
# File 'lib/rails/sharding/active_record_extensions.rb', line 42

def original_scope
  @original_scope
end

Instance Method Details

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

Delegates == to method_missing so that User.using_scope(:a,:b).where(:name => “Mike”) gets run in the correct shard context when #== is evaluated.



75
76
77
# File 'lib/rails/sharding/active_record_extensions.rb', line 75

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

#using_shard(shard_group, shard_name) ⇒ Object

if using_shard is called twice in a chain, just replaces configuration



51
52
53
54
55
# File 'lib/rails/sharding/active_record_extensions.rb', line 51

def using_shard(shard_group, shard_name)
  @shard_group = shard_group
  @shard_name = shard_name
  self
end