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



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



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
# File 'lib/active_record_shards/shard_support.rb', line 21

def find(*find_args)
  ensure_concrete!

  exception = nil
  enum.each do
    record = @scope.find(*find_args)
    return record if record
  rescue ActiveRecord::RecordNotFound => e
    exception = e
  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