Class: Octopus::ScopeProxy
- Inherits:
-
Object
- Object
- Octopus::ScopeProxy
- Defined in:
- lib/octopus/scope_proxy.rb
Instance Attribute Summary collapse
-
#klass ⇒ Object
Returns the value of attribute klass.
-
#shard ⇒ Object
Returns the value of attribute shard.
Instance Method Summary collapse
-
#==(*args) ⇒ Object
(also: #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.
- #connection ⇒ Object
-
#initialize(shard, klass) ⇒ ScopeProxy
constructor
A new instance of ScopeProxy.
- #method_missing(method, *args, &block) ⇒ Object
-
#transaction(options = {}, &block) ⇒ Object
Transaction Method send all queries to a specified shard.
- #using(shard) ⇒ Object
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
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/octopus/scope_proxy.rb', line 27 def method_missing(method, *args, &block) result = @klass.connection.run_queries_on_shard(@shard) do @klass.send(method, *args, &block) end if result.respond_to?(:scoped) @klass = result return self end result end |
Instance Attribute Details
#klass ⇒ Object
Returns the value of attribute klass.
2 3 4 |
# File 'lib/octopus/scope_proxy.rb', line 2 def klass @klass end |
#shard ⇒ Object
Returns the value of attribute shard.
2 3 4 |
# File 'lib/octopus/scope_proxy.rb', line 2 def shard @shard end |
Instance Method Details
#==(*args) ⇒ 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.
42 43 44 |
# File 'lib/octopus/scope_proxy.rb', line 42 def ==(*args) method_missing(:==, *args) end |
#connection ⇒ Object
22 23 24 25 |
# File 'lib/octopus/scope_proxy.rb', line 22 def connection @klass.connection().current_shard = @shard @klass.connection() end |
#transaction(options = {}, &block) ⇒ Object
Transaction Method send all queries to a specified shard.
16 17 18 19 20 |
# File 'lib/octopus/scope_proxy.rb', line 16 def transaction( = {}, &block) @klass.connection.run_queries_on_shard(@shard) do @klass = @klass.connection().transaction(, &block) end end |
#using(shard) ⇒ Object
9 10 11 12 13 |
# File 'lib/octopus/scope_proxy.rb', line 9 def using(shard) raise "Nonexistent Shard Name: #{shard}" if @klass.connection.instance_variable_get(:@shards)[shard].nil? @shard = shard return self end |