Class: ActiveRecordShards::ShardSupport
- Inherits:
-
Object
- Object
- ActiveRecordShards::ShardSupport
- Defined in:
- lib/active_record_shards/shard_support.rb
Defined Under Namespace
Classes: ShardEnumerator
Instance Method Summary collapse
- #count ⇒ Object
- #enum ⇒ Object
- #find(*find_args) ⇒ Object
-
#initialize(scope) ⇒ ShardSupport
constructor
A new instance of ShardSupport.
- #to_a ⇒ Object
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
#count ⇒ Object
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 |
#enum ⇒ Object
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_a ⇒ Object
40 41 42 |
# File 'lib/active_record_shards/shard_support.rb', line 40 def to_a enum.flat_map { @scope.clone.to_a } end |