Class: Simple::Sharding::ScopeProxy
- Inherits:
-
Object
- Object
- Simple::Sharding::ScopeProxy
- Defined in:
- lib/simple/sharding/active_record_extensions.rb
Overview
Return value of the #sharding scope method. Allows us to chain the shard choice with other ActiveRecord scopes
Instance Attribute Summary collapse
-
#original_scope ⇒ Object
Returns the value of attribute original_scope.
Instance Method Summary collapse
-
#==(other) ⇒ Object
(also: #eql?)
Delegates == to method_missing so that User.sharding(:id).where(:name => “Mike”) gets run in the correct shard context when #== is evaluated.
-
#initialize(shard_id, original_scope) ⇒ ScopeProxy
constructor
A new instance of ScopeProxy.
- #method_missing(method, *args, &block) ⇒ Object
- #sharding(shard_id) ⇒ Object
Constructor Details
#initialize(shard_id, original_scope) ⇒ ScopeProxy
Returns a new instance of ScopeProxy.
35 36 37 38 |
# File 'lib/simple/sharding/active_record_extensions.rb', line 35 def initialize(shard_id, original_scope) @shard_id = shard_id @original_scope = original_scope end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/simple/sharding/active_record_extensions.rb', line 45 def method_missing(method, *args, &block) res = Core.sharding(@shard_id) 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 res.respond_to?(:to_sql) @original_scope = res return self end # return res res end |
Instance Attribute Details
#original_scope ⇒ Object
Returns the value of attribute original_scope.
33 34 35 |
# File 'lib/simple/sharding/active_record_extensions.rb', line 33 def original_scope @original_scope end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
Delegates == to method_missing so that User.sharding(:id).where(:name => “Mike”) gets run in the correct shard context when #== is evaluated.
63 64 65 |
# File 'lib/simple/sharding/active_record_extensions.rb', line 63 def ==(other) method_missing(:==, other) end |
#sharding(shard_id) ⇒ Object
40 41 42 43 |
# File 'lib/simple/sharding/active_record_extensions.rb', line 40 def sharding(shard_id) @shard_id = shard_id self end |