Last active
August 29, 2015 14:01
-
-
Save backspace/d77d93c892da8c2577f9 to your computer and use it in GitHub Desktop.
Backports Criteria.none to Mongoid 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Mongoid | |
class Criteria | |
def none | |
@none = true and self | |
end | |
def empty_and_chainable? | |
!!@none | |
end | |
end | |
module Contextual | |
class None | |
include ::Enumerable | |
# Previously included Queryable, which has been extracted in v4 | |
attr_reader :collection, :criteria, :klass | |
def blank? | |
!exists? | |
end | |
alias :empty? :blank? | |
attr_reader :criteria, :klass | |
def ==(other) | |
other.is_a?(None) | |
end | |
def each | |
if block_given? | |
[].each { |doc| yield(doc) } | |
self | |
else | |
to_enum | |
end | |
end | |
def exists?; false; end | |
def initialize(criteria) | |
@criteria, @klass = criteria, criteria.klass | |
end | |
def last; nil; end | |
def length | |
entries.length | |
end | |
alias :size :length | |
end | |
private | |
def create_context | |
return None.new(self) if empty_and_chainable? | |
embedded ? Memory.new(self) : Mongo.new(self) | |
end | |
end | |
module Finders | |
delegate :none, to: :with_default_scope | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment