Module: Mongoid::Criteria::Findable
Relationships & Source Files | |
Extension / Inclusion / Inheritance Descendants | |
Included In:
| |
Defined in: | lib/mongoid/criteria/findable.rb |
Overview
Mixin module included in ::Mongoid::Criteria
which adds the ability to find document by id.
Instance Method Summary
-
#execute_or_raise(ids, multi) ⇒ Document | Array<Document>
Execute the criteria or raise an error if no documents found.
-
#find(*args) ⇒ Document | Array<Document>
Find the matching document(s) in the criteria for the provided id(s).
-
#for_ids(ids) ⇒ Criteria
Adds a criterion to the
::Mongoid::Criteria
that specifies an id that must be matched. -
#multiple_from_db(ids) ⇒ Array<Document>
Get the documents from the identity map, and if not found hit the database.
-
#from_database(ids) ⇒ Array<Document>
private
Internal use only
Internal use only
Get documents from the database only.
- #from_database_selector(ids) private
-
#id_finder ⇒ Symbol
private
Internal use only
Internal use only
Get the finder used to generate the id query.
-
#mongoize_ids(ids) ⇒ Array<Object>
private
Internal use only
Internal use only
Convert all the ids to their proper types.
-
#multi_args?(args) ⇒ true | false
private
Indicates whether the given arguments array is a list of values.
-
#prepare_ids_for_find(args) ⇒ Array
private
Convert args to the #find method into a flat array of ids.
-
#raise_invalid
private
Convenience method of raising an invalid find error.
Instance Method Details
#execute_or_raise(ids, multi) ⇒ Document | Array<Document>
Execute the criteria or raise an error if no documents found.
# File 'lib/mongoid/criteria/findable.rb', line 23
def execute_or_raise(ids, multi) result = multiple_from_db(ids) check_for_missing_documents!(result, ids) multi ? result : result.first end
#find(*args) ⇒ Document | Array<Document>
Each argument can be an individual id, an array of ids or a nested array. Each array will be flattened.
Find the matching document(s) in the criteria for the provided id(s).
# File 'lib/mongoid/criteria/findable.rb', line 43
def find(*args) ids = prepare_ids_for_find(args) raise_invalid if ids.any?(&:nil?) for_ids(ids).execute_or_raise(ids, multi_args?(args)) end
#for_ids(ids) ⇒ Criteria
Adds a criterion to the ::Mongoid::Criteria
that specifies an id that must be matched.
# File 'lib/mongoid/criteria/findable.rb', line 60
def for_ids(ids) ids = mongoize_ids(ids) if ids.size > 1 send(id_finder, { _id: { "$in" => ids }}) else send(id_finder, { _id: ids.first }) end end
#from_database(ids) ⇒ Array<Document> (private)
Get documents from the database only.
# File 'lib/mongoid/criteria/findable.rb', line 108
def from_database(ids) from_database_selector(ids).entries end
#from_database_selector(ids) (private)
[ GitHub ]# File 'lib/mongoid/criteria/findable.rb', line 112
def from_database_selector(ids) if ids.size > 1 any_in(_id: ids) else where(_id: ids.first) end end
#id_finder ⇒ Symbol (private)
Get the finder used to generate the id query.
# File 'lib/mongoid/criteria/findable.rb', line 94
def id_finder @id_finder ||= extract_id ? :all_of : :where end
#mongoize_ids(ids) ⇒ Array<Object
> (private)
Convert all the ids to their proper types.
# File 'lib/mongoid/criteria/findable.rb', line 130
def mongoize_ids(ids) ids.map do |id| id = id[:_id] if id.respond_to?(:keys) && id[:_id] klass.fields["_id"].mongoize(id) end end
#multi_args?(args) ⇒ true
| false
(private)
Indicates whether the given arguments array is a list of values. Used by the #find method to determine whether to return an array or single value.
# File 'lib/mongoid/criteria/findable.rb', line 168
def multi_args?(args) args.size > 1 || !args.first.is_a?(Hash) && args.first.resizable? end
#multiple_from_db(ids) ⇒ Array<Document>
Get the documents from the identity map, and if not found hit the database.
# File 'lib/mongoid/criteria/findable.rb', line 78
def multiple_from_db(ids) return entries if ids = mongoize_ids(ids) ids.empty? ? [] : from_database(ids) end
#prepare_ids_for_find(args) ⇒ Array (private)
Convert args to the #find method into a flat array of ids.
#raise_invalid (private)
Convenience method of raising an invalid find error.
# File 'lib/mongoid/criteria/findable.rb', line 178
def raise_invalid raise Errors::InvalidFind.new end