Module: Mongoid::Matchable
- Extended by:
- ActiveSupport::Concern
- Included in:
- Composable
- Defined in:
- lib/mongoid/matchable.rb,
lib/mongoid/matchable/gt.rb,
lib/mongoid/matchable/in.rb,
lib/mongoid/matchable/lt.rb,
lib/mongoid/matchable/ne.rb,
lib/mongoid/matchable/or.rb,
lib/mongoid/matchable/all.rb,
lib/mongoid/matchable/and.rb,
lib/mongoid/matchable/gte.rb,
lib/mongoid/matchable/lte.rb,
lib/mongoid/matchable/nin.rb,
lib/mongoid/matchable/nor.rb,
lib/mongoid/matchable/size.rb,
lib/mongoid/matchable/exists.rb,
lib/mongoid/matchable/regexp.rb,
lib/mongoid/matchable/default.rb,
lib/mongoid/matchable/elem_match.rb
Overview
This module contains all the behavior for ruby implementations of MongoDB selectors.
Defined Under Namespace
Classes: All, And, Default, ElemMatch, Exists, Gt, Gte, In, Lt, Lte, Ne, Nin, Nor, Or, Regexp, Size
Constant Summary collapse
- MATCHERS =
Hash lookup for the matcher for a specific operation.
{ "$all" => All, "$elemMatch" => ElemMatch, "$and" => And, "$exists" => Exists, "$gt" => Gt, "$gte" => Gte, "$in" => In, "$lt" => Lt, "$lte" => Lte, "$ne" => Ne, "$nin" => Nin, "$or" => Or, "$nor" => Nor, "$size" => Size }.with_indifferent_access.freeze
Class Method Summary collapse
-
.matcher(document, key, value) ⇒ Matcher
private
Get the matcher for the supplied key and value.
Instance Method Summary collapse
-
#_matches?(selector) ⇒ true, false
Determines if this document has the attributes to match the supplied MongoDB selector.
Class Method Details
.matcher(document, key, value) ⇒ Matcher
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Get the matcher for the supplied key and value. Will determine the class name from the key.
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/mongoid/matchable.rb', line 115 def matcher(document, key, value) if value.is_a?(Hash) matcher = MATCHERS[value.keys.first] if matcher matcher.new(extract_attribute(document, key)) else Default.new(extract_attribute(document, key)) end elsif value.regexp? Regexp.new(extract_attribute(document, key)) else case key.to_s when "$or" then Or.new(value, document) when "$and" then And.new(value, document) when "$nor" then Nor.new(value, document) else Default.new(extract_attribute(document, key)) end end end |
Instance Method Details
#_matches?(selector) ⇒ true, false
Determines if this document has the attributes to match the supplied MongoDB selector. Used for matching on embedded associations.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/mongoid/matchable.rb', line 59 def _matches?(selector) selector.each_pair do |key, value| if value.is_a?(Hash) value.each do |item| if item[0].to_s == "$not".freeze item = item[1] return false if matcher(self, key, item)._matches?(item) else return false unless matcher(self, key, Hash[*item])._matches?(Hash[*item]) end end else return false unless matcher(self, key, value)._matches?(value) end end true end |