Module: Mongoid::Matchers::Strategies

Extended by:
Strategies
Included in:
Strategies
Defined in:
lib/mongoid/matchers/strategies.rb

Overview

This module is responsible for returning the correct matcher given a MongoDB query expression.

Constant Summary collapse

MATCHERS =
{
  "$all" => Matchers::All,
  "$and" => Matchers::And,
  "$exists" => Matchers::Exists,
  "$gt" => Matchers::Gt,
  "$gte" => Matchers::Gte,
  "$in" => Matchers::In,
  "$lt" => Matchers::Lt,
  "$lte" => Matchers::Lte,
  "$ne" => Matchers::Ne,
  "$nin" => Matchers::Nin,
  "$or" => Matchers::Or,
  "$size" => Matchers::Size
}

Instance Method Summary collapse

Instance Method Details

#matcher(document, key, value) ⇒ Matcher

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:

Returns:

  • (Matcher)

    The matcher.

Since:

  • 2.0.0.rc.7



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/mongoid/matchers/strategies.rb', line 52

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
      when "$or" then Matchers::Or.new(value, document)
      when "$and" then Matchers::And.new(value, document)
      else Default.new(extract_attribute(document, key))
    end
  end
end