class ActiveRecordShards::ShardSupport

Public Class Methods

new(scope) click to toggle source
# File lib/active_record_shards/shard_support.rb, line 13
def initialize(scope)
  @scope = scope
end

Public Instance Methods

count() click to toggle source
# File lib/active_record_shards/shard_support.rb, line 36
def count
  enum.inject(0) { |accum, _shard| @scope.clone.count + accum }
end
enum() click to toggle source
# File lib/active_record_shards/shard_support.rb, line 17
def enum
  ShardEnumerator.new
end
find(*find_args) click to toggle source
# 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_a() click to toggle source
# File lib/active_record_shards/shard_support.rb, line 40
def to_a
  enum.flat_map { @scope.clone.to_a }
end

Private Instance Methods

ensure_concrete!() click to toggle source
# File lib/active_record_shards/shard_support.rb, line 46
def ensure_concrete!
  raise "Please call this method on a concrete model, not an abstract class!" if @scope.abstract_class?
end