module Mongoid::Criteria::Queryable::Extensions::Numeric::ClassMethods

Public Instance Methods

__numeric__(object) click to toggle source

Get the object as a numeric.

@api private

@example Get the object as numeric.

Object.__numeric__("1.442")

@param [ Object ] object The object to convert.

@return [ Object ] The converted number.

# File lib/mongoid/criteria/queryable/extensions/numeric.rb, line 46
def __numeric__(object)
  str = object.to_s
  raise ArgumentError if str.empty?

  # These requirements seem a bit odd, but they're explicitly specified in the tests,
  # so we're obligated to keep them, for now. (This code was rewritten from a one-line
  # regex, due to security concerns with a polynomial regex being used on uncontrolled
  # data).

  str = str.chop if str.end_with?('.')
  return 0 if str.empty?

  result = Integer(str) rescue Float(object)

  integer = result.to_i
  integer == result ? integer : result
end
evolve(object) click to toggle source

Evolve the object to an integer.

@example Evolve to integers.

Integer.evolve("1")

@param [ Object ] object The object to evolve.

@return [ Integer ] The evolved object.

# File lib/mongoid/criteria/queryable/extensions/numeric.rb, line 72
def evolve(object)
  __evolve__(object) do |obj|
    __numeric__(obj) rescue obj
  end
end