module Mongoid::Matcher::Size

In-memory matcher for $size expression.

@see www.mongodb.com/docs/manual/reference/operator/query/size/

@api private

Public Instance Methods

matches?(exists, value, condition) click to toggle source

Returns whether a value satisfies a $size expression.

@param [ true | false ] exists Not used. @param [ Numeric ] value The value to check. @param [ Integer | Array<Object> ] condition The $size condition

predicate, either a non-negative Integer or an Array to match size.

@return [ true | false ] Whether the value matches.

@api private

# File lib/mongoid/matcher/size.rb, line 22
                def matches?(exists, value, condition)
  case condition
  when Float
    raise Errors::InvalidQuery, "$size argument must be a non-negative integer: #{Errors::InvalidQuery.truncate_expr(condition)}"
  when Numeric
    if condition < 0
      raise Errors::InvalidQuery, "$size argument must be a non-negative integer: #{Errors::InvalidQuery.truncate_expr(condition)}"
    end
  else
    raise Errors::InvalidQuery, "$size argument must be a non-negative integer: #{Errors::InvalidQuery.truncate_expr(condition)}"
  end

  if Array === value
    value.length == condition
  else
    false
  end
end