Class: Octopus::RelationProxy

Inherits:
BasicObject
Includes:
ShardTracking::Attribute
Defined in:
lib/octopus/relation_proxy.rb

Defined Under Namespace

Modules: CaseFixer

Constant Summary collapse

ENUM_METHODS =

methods redefined in ActiveRecord that should run_on_shard

(::Enumerable.instance_methods - ::Object.instance_methods).reject do |m|
  ::ActiveRecord::Relation.instance_method(m).source_location rescue nil
end + [:each, :map, :index_by]
ENUM_WITH_BLOCK_METHODS =

`find { … }` etc. should run_on_shard, `find(id)` should be sent to relation

[:find, :select, :none?, :any?, :one?, :many?, :sum]
BATCH_METHODS =
[:find_each, :find_in_batches, :in_batches]
WHERE_CHAIN_METHODS =
[:not]

Instance Attribute Summary collapse

Attributes included from ShardTracking::Attribute

#current_shard

Instance Method Summary collapse

Methods included from ShardTracking::Attribute

included, #set_current_shard

Constructor Details

#initialize(shard, ar_relation) ⇒ RelationProxy

Returns a new instance of RelationProxy.


14
15
16
17
# File 'lib/octopus/relation_proxy.rb', line 14

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/octopus/relation_proxy.rb', line 32

def method_missing(method, *args, &block)
  if !block && BATCH_METHODS.include?(method)
    ::Enumerator.new do |yielder|
      run_on_shard do
        @ar_relation.public_send(method, *args) do |batch_item|
          yielder << batch_item
        end
      end
    end
  elsif ENUM_METHODS.include?(method) || block && ENUM_WITH_BLOCK_METHODS.include?(method)
    run_on_shard { @ar_relation.to_a }.public_send(method, *args, &block)
  elsif WHERE_CHAIN_METHODS.include?(method)
    ::Octopus::ScopeProxy.new(@current_shard, run_on_shard { @ar_relation.public_send(method, *args) } )
  elsif block
    @ar_relation.public_send(method, *args, &block)
  else
    run_on_shard do
      if method == :load_records
        @ar_relation.send(method, *args)
      else
        @ar_relation.public_send(method, *args)
      end
    end
  end
end

Instance Attribute Details

#ar_relationObject

Returns the value of attribute ar_relation


12
13
14
# File 'lib/octopus/relation_proxy.rb', line 12

def ar_relation
  @ar_relation
end

Instance Method Details

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


58
59
60
61
62
63
64
65
# File 'lib/octopus/relation_proxy.rb', line 58

def ==(other)
  case other
  when ::Octopus::RelationProxy
    method_missing(:==, other.ar_relation)
  else
    method_missing(:==, other)
  end
end

#respond_to?(*args) ⇒ Boolean

Returns:

  • (Boolean)

19
20
21
# File 'lib/octopus/relation_proxy.rb', line 19

def respond_to?(*args)
  method_missing(:respond_to?, *args)
end