123456789_123456789_123456789_123456789_123456789_

Module: Mongoid::Utils Private

Do not use. This module is for internal use only.
Relationships & Source Files
Defined in: lib/mongoid/utils.rb

Overview

Utility functions for ::Mongoid.

Constant Summary

  • PLACEHOLDER =

    A unique placeholder value that will never accidentally collide with valid values. This is useful as a default keyword argument value when you want the argument to be optional, but you also want to be able to recognize that the caller did not provide a value for it.

    # File 'lib/mongoid/utils.rb', line 14
    Object.new.freeze

Instance Method Summary

Instance Method Details

#monotonic_timeFloat

This function should be used if you need to measure time.

Examples:

Calculate elapsed time.

starting = Utils.monotonic_time
# do something time consuming
ending = Utils.monotonic_time
puts "It took #{(ending - starting).to_i} seconds"

Returns:

  • (Float)

    seconds according to monotonic clock

See Also:

[ GitHub ]

  
# File 'lib/mongoid/utils.rb', line 37

def monotonic_time
  Process.clock_gettime(Process::CLOCK_MONOTONIC)
end

#placeholder?(value) ⇒ true | false

Asks if the given value is a placeholder or not.

Parameters:

  • value (Object)

    the value to compare

Returns:

  • (true | false)

    if the value is a placeholder or not.

[ GitHub ]

  
# File 'lib/mongoid/utils.rb', line 21

def placeholder?(value)
  value == PLACEHOLDER
end

#truthy_string?(string) ⇒ true | false

Returns true if the string is any of the following values: “1”, “yes”, “true”, “on”. Anything else is assumed to be false. Case is ignored, as are leading or trailing spaces.

Parameters:

  • string (String)

    the string value to consider

[ GitHub ]

  
# File 'lib/mongoid/utils.rb', line 48

def truthy_string?(string)
  %w[ 1 yes true on ].include?(string.strip.downcase)
end