Class: Mongoid::Criteria
- Inherits:
-
Object
- Object
- Mongoid::Criteria
- Defined in:
- lib/workarea/ext/mongoid/except.rb,
lib/workarea/ext/mongoid/each_by.rb,
lib/workarea/ext/mongoid/lookup_hash.rb,
lib/workarea/ext/mongoid/find_ordered.rb
Instance Method Summary collapse
- #each_by(by, &block) ⇒ Object
- #each_slice_of(size, &block) ⇒ Object
- #except(id) ⇒ Object
- #find_ordered(*ids) ⇒ Object
- #to_lookup_hash ⇒ Object (also: #to_lookup_h)
Instance Method Details
#each_by(by, &block) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/workarea/ext/mongoid/each_by.rb', line 3 def each_by(by, &block) i = 0 total = 0 set_limit = [:limit] while (results = ordered_clone.skip(i).limit(by)) && results.exists? results.each do |result| return self if set_limit && set_limit >= total && total > 0 total += 1 yield result end i += by end self end |
#each_slice_of(size, &block) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/workarea/ext/mongoid/each_by.rb', line 22 def each_slice_of(size, &block) total = 0 set_limit = [:limit] while (results = ordered_clone.skip(total).limit(size)) && results.exists? total += size if set_limit && total > set_limit yield results.first(total - set_limit) return self else yield results end end self end |
#except(id) ⇒ Object
2 3 4 |
# File 'lib/workarea/ext/mongoid/except.rb', line 2 def except(id) where(:id.ne => id) end |
#find_ordered(*ids) ⇒ Object
3 4 5 6 7 8 9 |
# File 'lib/workarea/ext/mongoid/find_ordered.rb', line 3 def find_ordered(*ids) ids = Array(ids).flatten return [] if ids.blank? lookup = scoped.any_in(id: ids).to_lookup_hash ids.map { |id| lookup[id] }.compact end |
#to_lookup_hash ⇒ Object Also known as: to_lookup_h
16 17 18 19 20 |
# File 'lib/workarea/ext/mongoid/lookup_hash.rb', line 16 def to_lookup_hash scoped.each_with_object(LookupHash.new(klass)) do |model, result| result[model.id] = model end end |