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.



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

def initialize(scope)
  @scope = scope
end

Instance Method Details

#countObject



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

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

#enumObject



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

def enum
  ShardEnumerator.new
end

#find(*find_args) ⇒ Object



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

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



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

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