Module: Mongoid::Matchable

Extended by:
ActiveSupport::Concern
Included in:
Composable
Defined in:
build/mongoid-7.0/lib/mongoid/matchable.rb,
build/mongoid-7.0/lib/mongoid/matchable/eq.rb,
build/mongoid-7.0/lib/mongoid/matchable/gt.rb,
build/mongoid-7.0/lib/mongoid/matchable/in.rb,
build/mongoid-7.0/lib/mongoid/matchable/lt.rb,
build/mongoid-7.0/lib/mongoid/matchable/ne.rb,
build/mongoid-7.0/lib/mongoid/matchable/or.rb,
build/mongoid-7.0/lib/mongoid/matchable/all.rb,
build/mongoid-7.0/lib/mongoid/matchable/and.rb,
build/mongoid-7.0/lib/mongoid/matchable/gte.rb,
build/mongoid-7.0/lib/mongoid/matchable/lte.rb,
build/mongoid-7.0/lib/mongoid/matchable/nin.rb,
build/mongoid-7.0/lib/mongoid/matchable/nor.rb,
build/mongoid-7.0/lib/mongoid/matchable/size.rb,
build/mongoid-7.0/lib/mongoid/matchable/exists.rb,
build/mongoid-7.0/lib/mongoid/matchable/regexp.rb,
build/mongoid-7.0/lib/mongoid/matchable/default.rb,
build/mongoid-7.0/lib/mongoid/matchable/elem_match.rb

Overview

This module contains all the behavior for Ruby implementations of MongoDB selectors.

Since:

  • 4.0.0

Defined Under Namespace

Classes: All, And, Default, ElemMatch, Eq, 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.

Since:

  • 1.0.0

{
  "$all" => All,
  "$and" => And,
  "$elemMatch" => ElemMatch,
  "$eq" => Eq,
  "$exists" => Exists,
  "$gt" => Gt,
  "$gte" => Gte,
  "$in" => In,
  "$lt" => Lt,
  "$lte" => Lte,
  "$ne" => Ne,
  "$nin" => Nin,
  "$nor" => Nor,
  "$or" => Or,
  "$size" => Size,
}.with_indifferent_access.freeze

Class Method Summary collapse

Instance Method Summary collapse

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.

Examples:

Get the matcher.

Matchable.matcher(document, :title, { "$in" => [ "test" ] })

Parameters:

  • document (Document)

    The document to check.

  • key (Symbol, String)

    The field name.

  • value (Object, Hash)

    The value or selector.

Returns:

  • (Matcher)

    The matcher.

Since:

  • 2.0.0.rc.7



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'build/mongoid-7.0/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.

Examples:

Does the document match?

document._matches?(:title => { "$in" => [ "test" ] })

Parameters:

  • selector (Hash)

    The MongoDB selector.

Returns:

  • (true, false)

    True if matches, false if not.

Since:

  • 1.0.0



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'build/mongoid-7.0/lib/mongoid/matchable.rb', line 61

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(key, item)._matches?(item)
        else
          return false unless matcher(key, Hash[*item])._matches?(Hash[*item])
        end
      end
    else
      return false unless matcher(key, value)._matches?(value)
    end
  end
  true
end