module Mongo::Utils
@api private
Public Instance Methods
camelize(sym)
click to toggle source
# File lib/mongo/utils.rb, line 71 def camelize(sym) sym.to_s.gsub(/_(\w)/) { $1.upcase } end
excerpt_backtrace(exc, **opts)
click to toggle source
@option opts [ true | false | nil | Integer ] :bg_error_backtrace
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.
# File lib/mongo/utils.rb, line 49 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_time()
click to toggle source
This function should be used if you need to measure time. @example Calculate elapsed time.
starting = Utils.monotonic_time # do something time consuming ending = Utils.monotonic_time puts "It took #{(ending - starting).to_i} seconds"
@see blog.dnsimple.com/2018/03/elapsed-time-with-ruby-the-right-way/
@return [Float] seconds according to monotonic clock
# File lib/mongo/utils.rb, line 100 def monotonic_time Process.clock_gettime(Process::CLOCK_MONOTONIC) end
shallow_camelize_keys(hash)
click to toggle source
Stringifies the keys in the provided hash and converts underscore style keys to camel case style keys.
# File lib/mongo/utils.rb, line 67 def shallow_camelize_keys(hash) Hash[hash.map { |k, v| [camelize(k), v] }] end
shallow_symbolize_keys(hash)
click to toggle source
Symbolizes the keys in the provided hash.
# File lib/mongo/utils.rb, line 61 def shallow_symbolize_keys(hash) Hash[hash.map { |k, v| [k.to_sym, v] }] end
transform_server_api(server_api)
click to toggle source
@note server_api must have symbol keys or be a BSON::Document.
# File lib/mongo/utils.rb, line 76 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)
click to toggle source
@option opts [ true | false | nil | Integer ] :bg_error_backtrace
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.
@option opts [ Logger ] :logger A custom logger to use. @option opts [ String ] :log_prefix A custom log prefix to use when
logging.
# File lib/mongo/utils.rb, line 39 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