123456789_123456789_123456789_123456789_123456789_

Module: Mongoid::Criteria::Queryable::Extensions::Numeric::ClassMethods

Relationships & Source Files
Defined in: lib/mongoid/criteria/queryable/extensions/numeric.rb

Instance Method Summary

Instance Method Details

#__numeric__(object) ⇒ Object

This method is for internal use only.

Get the object as a numeric.

Examples:

Get the object as numeric.

Object.__numeric__("1.442")

Parameters:

  • object (Object)

    The object to convert.

Returns:

  • (Object)

    The converted number.

Raises:

  • (ArgumentError)
[ GitHub ]

  
# 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) ⇒ Integer

Evolve the object to an integer.

Examples:

Evolve to integers.

Integer.evolve("1")

Parameters:

  • object (Object)

    The object to evolve.

Returns:

  • (Integer)

    The evolved object.

[ GitHub ]

  
# File 'lib/mongoid/criteria/queryable/extensions/numeric.rb', line 72

def evolve(object)
  __evolve__(object) do |obj|
    __numeric__(obj) rescue obj
  end
end