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.

Since:

  • 4.0.0

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.

Since:

  • 1.0.0

{
  "$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

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.

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

Parameters:

  • document (Document)

    The document to check.

  • key (Symbol, String)

    The field name.

  • The (Object, Hash)

    value or selector.

Returns:

  • (Matcher)

    The matcher.

Since:

  • 2.0.0.rc.7


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.

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


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