Class: ActiveRecordShards::ShardSupport

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record_shards/shard_support.rb

Defined Under Namespace

Classes: ShardEnumerator

Instance Method Summary collapse

Constructor Details

#initialize(scope) ⇒ ShardSupport

Returns a new instance of ShardSupport.



12
13
14
# File 'lib/active_record_shards/shard_support.rb', line 12

def initialize(scope)
  @scope = scope
end

Instance Method Details

#countObject



35
36
37
# File 'lib/active_record_shards/shard_support.rb', line 35

def count
  enum.inject(0) { |accum, _shard| @scope.clone.count + accum }
end

#enumObject



16
17
18
# File 'lib/active_record_shards/shard_support.rb', line 16

def enum
  ShardEnumerator.new
end

#find(*find_args) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/active_record_shards/shard_support.rb', line 20

def find(*find_args)
  ensure_concrete!

  exception = nil
  enum.each do
    begin
      record = @scope.find(*find_args)
      return record if record
    rescue ActiveRecord::RecordNotFound => e
      exception = e
    end
  end
  raise exception
end

#to_aObject



39
40
41
# File 'lib/active_record_shards/shard_support.rb', line 39

def to_a
  enum.flat_map { @scope.clone.to_a }
end