Sha256: 1a44a3e95a30c6a959fd9a089db8bfdb81797f2647085264abe4dea0e7d8c619

Contents?: true

Size: 890 Bytes

Versions: 62

Compression:

Stored size: 890 Bytes

Contents

module Mongoid
  class Criteria
    def each_by(by, &block)
      i = 0
      total = 0
      set_limit = options[: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

    def each_slice_of(size, &block)
      total = 0
      set_limit = options[: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

    private

    def ordered_clone
      options[:sort] ? clone : clone.asc(:_id)
    end
  end
end

Version data entries

62 entries across 62 versions & 1 rubygems

Version Path
workarea-core-3.5.27 lib/workarea/ext/mongoid/each_by.rb
workarea-core-3.5.26 lib/workarea/ext/mongoid/each_by.rb
workarea-core-3.4.45 lib/workarea/ext/mongoid/each_by.rb
workarea-core-3.5.25 lib/workarea/ext/mongoid/each_by.rb
workarea-core-3.5.23 lib/workarea/ext/mongoid/each_by.rb
workarea-core-3.4.44 lib/workarea/ext/mongoid/each_by.rb
workarea-core-3.5.22 lib/workarea/ext/mongoid/each_by.rb
workarea-core-3.4.43 lib/workarea/ext/mongoid/each_by.rb
workarea-core-3.5.21 lib/workarea/ext/mongoid/each_by.rb
workarea-core-3.4.42 lib/workarea/ext/mongoid/each_by.rb
workarea-core-3.5.20 lib/workarea/ext/mongoid/each_by.rb
workarea-core-3.4.41 lib/workarea/ext/mongoid/each_by.rb
workarea-core-3.5.19 lib/workarea/ext/mongoid/each_by.rb
workarea-core-3.4.40 lib/workarea/ext/mongoid/each_by.rb
workarea-core-3.5.18 lib/workarea/ext/mongoid/each_by.rb
workarea-core-3.4.39 lib/workarea/ext/mongoid/each_by.rb
workarea-core-3.5.17 lib/workarea/ext/mongoid/each_by.rb
workarea-core-3.4.38 lib/workarea/ext/mongoid/each_by.rb
workarea-core-3.5.16 lib/workarea/ext/mongoid/each_by.rb
workarea-core-3.4.37 lib/workarea/ext/mongoid/each_by.rb