Module: Mongo::Utils Private

Defined in:
lib/mongo/utils.rb

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Defined Under Namespace

Classes: LocalLogger

Class Method Summary collapse

Class Method Details

.camelize(sym) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



72
73
74
# File 'lib/mongo/utils.rb', line 72

module_function def camelize(sym)
  sym.to_s.gsub(/_(\w)/) { $1.upcase }
end

.excerpt_backtrace(exc, **opts) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Options Hash (**opts):

  • :bg_error_backtrace (true | false | nil | Integer)

    Experimental. Set to true to log complete backtraces for errors in background threads. Set to false or nil to not log backtraces. Provide a positive integer to log up to that many backtrace lines.



50
51
52
53
54
55
56
57
58
59
# File 'lib/mongo/utils.rb', line 50

module_function def excerpt_backtrace(exc, **opts)
  case lines = opts[:bg_error_backtrace]
  when Integer
    ":\n#{exc.backtrace[0..lines].join("\n")}"
  when false, nil
    nil
  else
    ":\n#{exc.backtrace.join("\n")}"
  end
end

.monotonic_timeFloat

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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:



101
102
103
# File 'lib/mongo/utils.rb', line 101

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

.shallow_camelize_keys(hash) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Stringifies the keys in the provided hash and converts underscore style keys to camel case style keys.



68
69
70
# File 'lib/mongo/utils.rb', line 68

module_function def shallow_camelize_keys(hash)
  Hash[hash.map { |k, v| [camelize(k), v] }]
end

.shallow_symbolize_keys(hash) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Symbolizes the keys in the provided hash.



62
63
64
# File 'lib/mongo/utils.rb', line 62

module_function def shallow_symbolize_keys(hash)
  Hash[hash.map { |k, v| [k.to_sym, v] }]
end

.transform_server_api(server_api) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

server_api must have symbol keys or be a BSON::Document.



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/mongo/utils.rb', line 77

module_function def transform_server_api(server_api)
  {}.tap do |doc|
    if version = server_api[:version]
      doc['apiVersion'] = version
    end
    unless server_api[:strict].nil?
      doc['apiStrict'] = server_api[:strict]
    end
    unless server_api[:deprecation_errors].nil?
      doc['apiDeprecationErrors'] = server_api[:deprecation_errors]
    end
  end
end

.warn_bg_exception(msg, exc, **opts) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Options Hash (**opts):

  • :bg_error_backtrace (true | false | nil | Integer)

    Experimental. Set to true to log complete backtraces for errors in background threads. Set to false or nil to not log backtraces. Provide a positive integer to log up to that many backtrace lines.

  • :logger (Logger)

    A custom logger to use.

  • :log_prefix (String)

    A custom log prefix to use when logging.



40
41
42
43
44
# File 'lib/mongo/utils.rb', line 40

module_function def warn_bg_exception(msg, exc, **opts)
  bt_excerpt = excerpt_backtrace(exc, **opts)
  logger = LocalLogger.new(**opts)
  logger.log_warn("#{msg}: #{exc.class}: #{exc}#{bt_excerpt}")
end