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/size.rb,
lib/mongoid/matchable/exists.rb,
lib/mongoid/matchable/default.rb
Overview
This module contains all the behavior for ruby implementations of MongoDB selectors.
Defined Under Namespace
Classes: All, And, Default, Exists, Gt, Gte, In, Lt, Lte, Ne, Nin, Or, Size
Constant Summary collapse
- MATCHERS =
Hash lookup for the matcher for a specific operation.
{ "$all" => All, "$and" => And, "$exists" => Exists, "$gt" => Gt, "$gte" => Gte, "$in" => In, "$lt" => Lt, "$lte" => Lte, "$ne" => Ne, "$nin" => Nin, "$or" => Or, "$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.
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/mongoid/matchable.rb', line 105 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 else case key.to_s when "$or" then Or.new(value, document) when "$and" then And.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.
54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/mongoid/matchable.rb', line 54 def matches?(selector) selector.each_pair do |key, value| if value.is_a?(Hash) value.each do |item| return false unless matcher(self, key, Hash[*item]).matches?(Hash[*item]) end else return false unless matcher(self, key, value).matches?(value) end end true end |