123456789_123456789_123456789_123456789_123456789_

Class: Mongo::URI::OptionsMapper Private

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Instance Chain:
Inherits: Object
Defined in: lib/mongo/uri/options_mapper.rb

Overview

Performs mapping between ::Mongo::URI options and Ruby options.

This class contains:

  • The mapping defining how ::Mongo::URI options are converted to Ruby options.
  • The mapping from downcased ::Mongo::URI option names to canonical-cased ::Mongo::URI option names.
  • Methods to perform conversion of ::Mongo::URI option values to Ruby option values (the convert_* methods). These generally warn and return nil when input given is invalid.
  • Methods to perform conversion of Ruby option values to standardized MongoClient options (revert_* methods). These assume the input is valid and generally do not perform validation.

::Mongo::URI option names are case insensitive. Ruby options are specified as symbols (though in ::Mongo::Client options use indifferent access).

Since:

  • 2.0.0

Constant Summary

::Mongo::Loggable - Included

PREFIX

Class Method Summary

Instance Attribute Summary

Instance Method Summary

::Mongo::Loggable - Included

#log_debug

Convenience method to log debug messages with the standard prefix.

#log_error

Convenience method to log error messages with the standard prefix.

#log_fatal

Convenience method to log fatal messages with the standard prefix.

#log_info

Convenience method to log info messages with the standard prefix.

#log_warn

Convenience method to log warn messages with the standard prefix.

#logger

Get the logger instance.

#_mongo_log_prefix, #format_message

Constructor Details

.new(**opts) ⇒ OptionsMapper

Instantiates the options mapper.

Parameters:

  • opts (Hash) —

    a customizable set of options

Options Hash (**opts):

  • :logger (Logger) —

    A custom logger to use.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/uri/options_mapper.rb', line 43

def initialize(**opts)
  @options = opts
end

Class Method Details

.uri_option(uri_key, name, **extra)

Simple internal dsl to register a MongoDB URI option in the URI_OPTION_MAP.

Parameters:

  • uri_key (String) —

    The MongoDB ::Mongo::URI option to register.

  • name (Symbol) —

    The name of the option in the driver.

  • extra (Hash) —

    Extra options.

    • :group [ ::Symbol ] Nested hash where option will go.
    • :type [ ::Symbol ] Name of function to transform value.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/uri/options_mapper.rb', line 210

def self.uri_option(uri_key, name, **extra)
  URI_OPTION_MAP[uri_key.downcase] = { name: name }.update(extra)
  URI_OPTION_CANONICAL_NAMES[uri_key.downcase] = uri_key
end

Instance Attribute Details

#options ⇒ Hash (readonly)

Returns:

  • (Hash) —

    The options.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/uri/options_mapper.rb', line 48

attr_reader :options

Instance Method Details

#add_uri_option(key, value, uri_options, offset: nil)

Adds an option to the uri options hash.

Acquires a target for the option based on group.
Transforms the value.
Merges the option into the target.

Parameters:

  • key (String) —

    ::Mongo::URI option name.

  • value (String) —

    The value of the option.

  • uri_options (Hash) —

    The base option target.

  • offset (Integer | nil) —

    Position of the option within the connection options segment, if known. Used to make warnings about unknown options actionable without quoting the key.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/uri/options_mapper.rb', line 62

def add_uri_option(key, value, uri_options, offset: nil)
  strategy = URI_OPTION_MAP[key.downcase]
  if strategy.nil?
    log_warn(unsupported_option_message(offset))
    return
  end

  canonical_key = URI_OPTION_CANONICAL_NAMES[key.downcase]
  group = strategy[:group]
  target = if group
             uri_options[group] || {}
           else
             uri_options
           end
  value = apply_transform(canonical_key, value, strategy[:type])
  # Sometimes the value here would be nil, for example if we are processing
  # read preference tags or auth mechanism properties and all of the
  # data within is invalid. Ignore such options.
  merge_uri_option(target, value, strategy[:name], canonical_key) unless value.nil?

  return unless group && !target.empty? && !uri_options.key?(group)

  uri_options[group] = target
end

#apply_transform(key, value, type) (private)

Applies URI value transformation by either using the default cast or a transformation appropriate for the given type.

Parameters:

  • key (String) —

    ::Mongo::URI option name.

  • value (String) —

    The value to be transformed.

  • type (Symbol) —

    The transform method.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/uri/options_mapper.rb', line 223

def apply_transform(key, value, type)
  if type
    send("convert_#{type}", key, value)
  else
    value
  end
end

#convert_array(_name, value) ⇒ Array<String> (private)

Extract values from the string and put them into an array.

Parameters:

  • name (String) —

    Name of the ::Mongo::URI option being processed.

  • value (String) —

    The string to build an array from.

Returns:

  • (Array<String>) —

    The array built from the string.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/uri/options_mapper.rb', line 570

def convert_array(_name, value)
  value.split(',')
end

#convert_auth_mech(name, value) ⇒ Symbol (private)

Authentication mechanism transformation.

Parameters:

  • name (String) —

    Name of the ::Mongo::URI option being processed.

  • value (String) —

    The authentication mechanism.

Returns:

  • (Symbol) —

    The transformed authentication mechanism.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/uri/options_mapper.rb', line 598

def convert_auth_mech(name, value)
  auth_mech = AUTH_MECH_MAP[value.upcase]
  (auth_mech || value).tap do |_mech|
    log_warn("#{name} is not a valid authentication mechanism") unless auth_mech
  end
end

#convert_auth_mech_props(_name, value) ⇒ Hash | nil (private)

::Mongo::Auth mechanism properties extractor.

Parameters:

  • name (String) —

    Name of the ::Mongo::URI option being processed.

  • value (String) —

    The auth mechanism properties string.

Returns:

  • (Hash | nil) —

    The auth mechanism properties hash.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/uri/options_mapper.rb', line 638

def convert_auth_mech_props(_name, value)
  properties = hash_extractor('authMechanismProperties', value, invalidate_on_error: true)
  if properties
    properties.each do |k, v|
      properties[k] = (v.downcase == 'true') if k.to_s.downcase == 'canonicalize_host_name' && v
    end
  end
  properties
end

#convert_bool(name, value) ⇒ true | false | nil (private)

Converts value to a boolean.

Returns true for 'true', false for 'false', otherwise nil.

Parameters:

  • name (String) —

    Name of the ::Mongo::URI option being processed.

  • value (String | true | false) —

    ::Mongo::URI option value.

Returns:

  • (true | false | nil) —

    Converted value.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/uri/options_mapper.rb', line 347

def convert_bool(name, value)
  case value
  when true, 'true', 'TRUE'
    true
  when false, 'false', 'FALSE'
    false
  else
    log_warn("Invalid boolean value for #{name}")
    nil
  end
end

#convert_integer(name, value) ⇒ nil | Integer (private)

Converts value into an integer. Only converts positive integers.

If the value is not a valid integer, warns and returns nil.

Parameters:

  • name (String) —

    Name of the ::Mongo::URI option being processed.

  • value (String | Integer) —

    ::Mongo::URI option value.

Returns:

  • (nil | Integer) —

    Converted value.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/uri/options_mapper.rb', line 466

def convert_integer(name, value)
  if value.is_a?(String) && /\A\d+\z/ !~ value
    log_warn("#{name} is not a valid integer")
    return nil
  end

  value.to_i
end

#convert_inverse_bool(name, value) ⇒ true | false | nil (private)

Parses a boolean value and returns its inverse.

Parameters:

  • name (String) —

    Name of the ::Mongo::URI option being processed.

  • value (String | true | false) —

    The URI option value.

Returns:

  • (true | false | nil) —

    The inverse of the boolean value parsed out, otherwise nil (and a warning will be logged).

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/uri/options_mapper.rb', line 430

def convert_inverse_bool(name, value)
  b = convert_bool(name, value)

  if b.nil?
    nil
  else
    !b
  end
end

#convert_max_staleness(name, value) ⇒ Integer | nil (private)

Parses the max staleness value, which must be either "0" or an integer greater or equal to 90.

Parameters:

  • name (String) —

    Name of the ::Mongo::URI option being processed.

  • value (String | Integer) —

    The max staleness string.

Returns:

  • (Integer | nil) —

    The max staleness integer parsed out if it is valid, otherwise nil (and a warning will be logged).

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/uri/options_mapper.rb', line 676

def convert_max_staleness(name, value)
  int = if value.is_a?(String) && /\A-?\d+\z/ =~ value
          value.to_i
        elsif value.is_a?(Integer)
          value
        end

  if int.nil?
    log_warn("Invalid value for #{name}")
    return nil
  end

  int = nil if int == -1

  if int && ((int > 0 && int < 90) || int < 0)
    log_warn("#{name} should be either 0 or greater than 90")
    int = nil
  end

  int
end

#convert_ms(name, value) ⇒ Float (private)

Ruby's convention is to provide timeouts in seconds, not milliseconds and to use fractions where more precision is necessary. The connection string options are always in MS so we provide an easy conversion type.

Parameters:

  • name (String) —

    Name of the ::Mongo::URI option being processed.

  • value (String | Integer | Float) —

    The millisecond value.

Returns:

  • (Float) —

    The seconds value.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/uri/options_mapper.rb', line 503

def convert_ms(name, value)
  case value
  when String
    unless /\A-?\d+(\.\d+)?\z/.match?(value)
      log_warn("Invalid value for #{name}")
      return nil
    end
    if value.to_s[0] == '-'
      log_warn("#{name} cannot be a negative number")
      return nil
    end
  when Integer, Float
    if value < 0
      log_warn("#{name} cannot be a negative number")
      return nil
    end
  else
    raise ArgumentError, "Can only convert Strings, Integers, or Floats to ms. Given: #{value.class}"
  end

  value.to_f / 1000
end

#convert_read_mode(_name, value) ⇒ Symbol | String (private)

Read preference mode transformation.

Parameters:

  • name (String) —

    Name of the ::Mongo::URI option being processed.

  • value (String) —

    The read mode string value.

Returns:

  • (Symbol | String) —

    The read mode.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/uri/options_mapper.rb', line 722

def convert_read_mode(_name, value)
  READ_MODE_MAP[value.downcase] || value
end

#convert_read_set(_name, value) ⇒ Hash (private)

Read preference tag set extractor.

Parameters:

  • name (String) —

    Name of the ::Mongo::URI option being processed.

  • value (String) —

    The tag set string.

Returns:

  • (Hash) —

    The tag set hash.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/uri/options_mapper.rb', line 798

def convert_read_set(_name, value)
  hash_extractor('readPreferenceTags', value)
end

#convert_read_tags(name, value) ⇒ Array<Hash> | nil (private)

Read preference tags transformation.

Parameters:

  • name (String) —

    Name of the ::Mongo::URI option being processed.

  • value (String) —

    The string representing tag set.

Returns:

  • (Array<Hash> | nil) —

    Array with tag set.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/uri/options_mapper.rb', line 767

def convert_read_tags(name, value)
  converted = convert_read_set(name, value)
  return unless converted

  [ converted ]
end

#convert_repeated_bool(_name, value) ⇒ Array<true | false | nil> (private)

Converts the value into a boolean and returns it wrapped in an array.

tls and ssl are the only repeated_bool options. An invalid value is preserved as a nil element rather than warned-and-dropped, so that URI#validate_uri_options! can detect it and raise instead of silently falling back to a plaintext connection (RUBY-3832).

Parameters:

Returns:

  • (Array<true | false | nil>) —

    The boolean value parsed and wrapped in an array.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/uri/options_mapper.rb', line 389

def convert_repeated_bool(_name, value)
  case value
  when true, 'true', 'TRUE'
    [ true ]
  when false, 'false', 'FALSE'
    [ false ]
  else
    [ nil ]
  end
end

#convert_server_monitoring_mode(name, value) ⇒ Symbol | nil (private)

::Mongo::Server monitoring mode transformation.

Parameters:

  • name (String) —

    Name of the ::Mongo::URI option being processed.

  • value (String) —

    The server monitoring mode string value.

Returns:

  • (Symbol | nil) —

    The server monitoring mode symbol.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/uri/options_mapper.rb', line 742

def convert_server_monitoring_mode(name, value)
  mode = value.downcase
  if SERVER_MONITORING_MODES.include?(mode)
    mode.to_sym
  else
    log_warn("#{name} is not a valid server monitoring mode")
    nil
  end
end

#convert_symbol(_name, value) ⇒ Symbol (private)

Converts value into a symbol.

Parameters:

Returns:

  • (Symbol) —

    Converted value.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/uri/options_mapper.rb', line 550

def convert_symbol(_name, value)
  value.to_sym
end

#convert_w(_name, value) ⇒ Integer | Symbol | String (private)

Converts value as a write concern.

If value is the word "majority", returns the symbol :majority. If value is a number, returns the number as an integer. Otherwise returns the string value unchanged.

Parameters:

  • name (String) —

    Name of the ::Mongo::URI option being processed.

  • value (String | Integer) —

    ::Mongo::URI option value.

Returns:

  • (Integer | Symbol | String) —

    Converted value.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/uri/options_mapper.rb', line 812

def convert_w(_name, value)
  case value
  when 'majority'
    :majority
  when /\A[0-9]+\z/
    value.to_i
  else
    value
  end
end

#convert_zlib_compression_level(name, value) ⇒ Integer | nil (private)

Parses the zlib compression level.

Parameters:

  • name (String) —

    Name of the ::Mongo::URI option being processed.

  • value (String | Integer) —

    The zlib compression level string.

Returns:

  • (Integer | nil) —

    The compression level value if it is between -1 and 9 (inclusive), otherwise nil (and a warning will be logged).

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/uri/options_mapper.rb', line 853

def convert_zlib_compression_level(name, value)
  i = if value.is_a?(String) && /\A-?\d+\z/ =~ value
        value.to_i
      elsif value.is_a?(Integer)
        value
      end

  if i && i >= -1 && i <= 9
    i
  else
    log_warn("#{name} is not a valid compression level")
    nil
  end
end

#hash_extractor(name, value, invalidate_on_error: false) ⇒ Hash | nil (private)

Extract values from the string and put them into a nested hash.

Multiple key-value pairs are delimited by a comma. Within each pair, the key is everything up to the first colon and the value is everything after it, so values may themselves contain colons (for example TOKEN_RESOURCE:mongodb://host).

Parameters:

  • name (String) —

    Name of the ::Mongo::URI option being processed.

  • value (String) —

    The string to build a hash from.

  • invalidate_on_error (true | false) —

    When a pair is malformed (has no value), discard the whole option and return nil instead of skipping just that pair. Used for options such as authMechanismProperties whose values must not contain commas.

Returns:

  • (Hash | nil) —

    The hash built from the string, or nil.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/uri/options_mapper.rb', line 901

def hash_extractor(name, value, invalidate_on_error: false)
  h = {}
  value.split(',').each do |tag|
    k, v = tag.split(':', 2)
    if v.nil?
      log_warn("Invalid value for #{name}")
      return nil if invalidate_on_error

      next
    end

    h[k.to_sym] = v
  end
  if h.empty?
    nil
  else
    h
  end
end

#merge_uri_option(target, value, name, canonical_key) (private)

Merges a new option into the target.

If the option exists at the target destination the merge will be an addition.

Specifically required to append an additional tag set to the array of tag sets without overwriting the original.

Parameters:

  • target (Hash) —

    The destination.

  • value (Object) —

    The value to be merged.

  • name (Symbol) —

    The name of the option.

  • canonical_key (String) —

    The canonical ::Mongo::URI option name.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/uri/options_mapper.rb', line 243

def merge_uri_option(target, value, name, canonical_key)
  if target.key?(name)
    if REPEATABLE_OPTIONS.include?(name)
      target[name] += value
    else
      log_warn("Repeated option key: #{canonical_key}.")
    end
  else
    target.merge!(name => value)
  end
end

#revert_array(value) ⇒ Array<String> (private)

Reverts an array.

Parameters:

  • value (Array<String>) —

    An array of strings.

Returns:

  • (Array<String>) —

    The passed value.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/uri/options_mapper.rb', line 579

def revert_array(value)
  value
end

#revert_auth_mech(value) ⇒ String (private)

Reverts auth mechanism.

Parameters:

  • value (Symbol) —

    The auth mechanism.

Returns:

  • (String) —

    The auth mechanism as a string.

Raises:

  • (ArgumentError) —

    if its an invalid auth mechanism.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/uri/options_mapper.rb', line 612

def revert_auth_mech(value)
  found = AUTH_MECH_MAP.detect do |_k, v|
    v == value
  end
  raise ArgumentError, "Unknown auth mechanism #{value}" unless found

  found.first
end

#revert_auth_mech_props(value) ⇒ Hash | nil (private)

Reverts auth mechanism properties.

Parameters:

  • value (Hash | nil) —

    The auth mech properties.

Returns:

  • (Hash | nil) —

    The passed value.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/uri/options_mapper.rb', line 653

def revert_auth_mech_props(value)
  value
end

#revert_bool(value) ⇒ true | false | nil (private)

Reverts a boolean type.

Parameters:

  • value (true | false | nil) —

    The boolean to revert.

Returns:

  • (true | false | nil) —

    The passed value.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/uri/options_mapper.rb', line 364

def revert_bool(value)
  value
end

#revert_integer(value) ⇒ Integer | nil (private)

Reverts an integer.

Parameters:

  • value (Integer | nil) —

    The integer.

Returns:

  • (Integer | nil) —

    The passed value.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/uri/options_mapper.rb', line 480

def revert_integer(value)
  value
end

#revert_inverse_bool(value) ⇒ true | false | nil (private)

Reverts and inverts a boolean type.

Parameters:

  • value (true | false | nil) —

    The boolean to revert and invert.

Returns:

  • (true | false | nil) —

    The inverted boolean.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/uri/options_mapper.rb', line 445

def revert_inverse_bool(value)
  value.nil? ? nil : !value
end

#revert_max_staleness(value) ⇒ Integer | nil (private)

Reverts max staleness.

Parameters:

  • value (Integer | nil) —

    The max staleness.

Returns:

  • (Integer | nil) —

    The passed value.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/uri/options_mapper.rb', line 703

def revert_max_staleness(value)
  value
end

#revert_ms(value) ⇒ Integer (private)

Reverts an ms.

Parameters:

  • value (Float) —

    The float.

Returns:

  • (Integer) —

    The number multiplied by 1000 as an integer.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/uri/options_mapper.rb', line 531

def revert_ms(value)
  (value * 1000).round
end

#revert_read_mode(value) ⇒ String (private) Also known as: #stringify_read_mode

Reverts read mode.

Parameters:

  • value (Symbol | String) —

    The read mode.

Returns:

  • (String) —

    The read mode as a string.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/uri/options_mapper.rb', line 731

def revert_read_mode(value)
  value.to_s.gsub(/_(\w)/) { ::Regexp.last_match(1).upcase }
end

#revert_read_tags(value) ⇒ Array<Hash> | nil (private)

Reverts read tags.

Parameters:

  • value (Array<Hash> | nil) —

    The read tags.

Returns:

  • (Array<Hash> | nil) —

    The passed value.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/uri/options_mapper.rb', line 779

def revert_read_tags(value)
  value
end

#revert_repeated_bool(value) ⇒ Array<true | false> | true | false | nil (private)

Reverts a repeated boolean type.

Parameters:

  • value (Array<true | false> | true | false | nil) —

    The repeated boolean to revert.

Returns:

  • (Array<true | false> | true | false | nil) —

    The passed value.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/uri/options_mapper.rb', line 405

def revert_repeated_bool(value)
  value
end

#revert_symbol(value) ⇒ String (private) Also known as: #stringify_symbol

Reverts a symbol.

Parameters:

  • value (Symbol) —

    The symbol.

Returns:

  • (String) —

    The passed value as a string.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/uri/options_mapper.rb', line 559

def revert_symbol(value)
  value.to_s
end

#revert_w(value) ⇒ Integer | String (private)

Reverts write concern.

Parameters:

  • value (Integer | Symbol | String) —

    The write concern.

Returns:

  • (Integer | String) —

    The write concern as a string.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/uri/options_mapper.rb', line 828

def revert_w(value)
  case value
  when Symbol
    value.to_s
  else
    value
  end
end

#revert_zlib_compression_level(value) ⇒ Integer | nil (private)

Reverts zlib compression level

Parameters:

  • value (Integer | nil) —

    The write concern.

Returns:

  • (Integer | nil) —

    The passed value.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/uri/options_mapper.rb', line 873

def revert_zlib_compression_level(value)
  value
end

#ruby_to_smc(opts) ⇒ Hash

Converts Ruby options provided to "standardized MongoClient options".

Parameters:

  • opts (Hash) —

    Ruby options to convert.

Returns:

  • (Hash) —

    Standardized MongoClient options.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/uri/options_mapper.rb', line 122

def ruby_to_smc(opts)
  rv = {}
  URI_OPTION_MAP.each do |uri_key, spec|
    if spec[:group]
      v = opts[spec[:group]]
      v &&= v[spec[:name]]
    else
      v = opts[spec[:name]]
    end
    next if v.nil?

    if (type = spec[:type])
      v = send("revert_#{type}", v)
    end
    canonical_key = URI_OPTION_CANONICAL_NAMES[uri_key]
    raise ArgumentError, "Option #{uri_key} is not known" unless canonical_key

    rv[canonical_key] = v
  end
  # For options that default to true, remove the value if it is true.
  %w[retryReads retryWrites].each do |k|
    rv.delete(k) if rv[k]
  end
  # Remove auth source when it is $external for mechanisms that default
  # (or require) that auth source.
  rv.delete('authSource') if %w[MONGODB-AWS].include?(rv['authMechanism']) && rv['authSource'] == '$external'
  # ssl and tls are aliases, remove ssl ones
  rv.delete('ssl')
  # TODO: remove authSource if it is the same as the database,
  # requires this method to know the database specified in the client.
  rv
end

#ruby_to_string(opts) ⇒ Hash

Converts Ruby options provided to their representation in a ::Mongo::URI string.

Parameters:

  • opts (Hash) —

    Ruby options to convert.

Returns:

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/uri/options_mapper.rb', line 160

def ruby_to_string(opts)
  rv = {}
  URI_OPTION_MAP.each do |uri_key, spec|
    if spec[:group]
      v = opts[spec[:group]]
      v &&= v[spec[:name]]
    else
      v = opts[spec[:name]]
    end
    next if v.nil?

    if (type = spec[:type])
      v = send("stringify_#{type}", v)
    end
    canonical_key = URI_OPTION_CANONICAL_NAMES[uri_key]
    raise ArgumentError, "Option #{uri_key} is not known" unless canonical_key

    rv[canonical_key] = v
  end
  # For options that default to true, remove the value if it is true.
  %w[retryReads retryWrites].each do |k|
    rv.delete(k) if rv[k]
  end
  # Remove auth source when it is $external for mechanisms that default
  # (or require) that auth source.
  rv.delete('authSource') if %w[MONGODB-AWS].include?(rv['authMechanism']) && rv['authSource'] == '$external'
  # ssl and tls are aliases, remove ssl ones
  rv.delete('ssl')
  # TODO: remove authSource if it is the same as the database,
  # requires this method to know the database specified in the client.
  rv
end

#smc_to_ruby(opts)

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/uri/options_mapper.rb', line 87

def smc_to_ruby(opts)
  uri_options = {}

  opts.each do |key, value|
    strategy = URI_OPTION_MAP[key.downcase]
    if strategy.nil?
      log_warn(unsupported_option_message)
      next
    end

    canonical_key = URI_OPTION_CANONICAL_NAMES[key.downcase]
    group = strategy[:group]
    target = if group
               uri_options[group] || {}
             else
               uri_options
             end

    value = apply_transform(canonical_key, value, strategy[:type])
    # Sometimes the value here would be nil, for example if we are processing
    # read preference tags or auth mechanism properties and all of the
    # data within is invalid. Ignore such options.
    merge_uri_option(target, value, strategy[:name], canonical_key) unless value.nil?

    uri_options[group] = target if group && !target.empty? && !uri_options.key?(group)
  end

  uri_options
end

#stringify_array(value) ⇒ String (private)

Stringifies an array.

Parameters:

  • value (Array<String>) —

    An array of strings.

Returns:

  • (String) —

    The array joined by commas.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/uri/options_mapper.rb', line 588

def stringify_array(value)
  value.join(',')
end

#stringify_auth_mech(value) ⇒ String | nil (private)

Stringifies auth mechanism.

Parameters:

  • value (Symbol) —

    The auth mechanism.

Returns:

  • (String | nil) —

    The auth mechanism as a string.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/uri/options_mapper.rb', line 626

def stringify_auth_mech(value)
  revert_auth_mech(value)
rescue StandardError
  nil
end

#stringify_auth_mech_props(value) ⇒ String | nil (private)

Stringifies auth mechanism properties.

Parameters:

  • value (Hash | nil) —

    The auth mech properties.

Returns:

  • (String | nil) —

    The string.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/uri/options_mapper.rb', line 662

def stringify_auth_mech_props(value)
  return if value.nil?

  value.map { |k, v| "#{k}:#{v}" }.join(',')
end

#stringify_bool(value) ⇒ String | nil (private)

Stringifies a boolean type.

Parameters:

  • value (true | false | nil) —

    The boolean.

Returns:

  • (String | nil) —

    The string.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/uri/options_mapper.rb', line 373

def stringify_bool(value)
  revert_bool(value)&.to_s
end

#stringify_integer(value) ⇒ String | nil (private)

Stringifies an integer.

Parameters:

  • value (Integer | nil) —

    The integer.

Returns:

  • (String | nil) —

    The string.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/uri/options_mapper.rb', line 489

def stringify_integer(value)
  revert_integer(value)&.to_s
end

#stringify_inverse_bool(value) ⇒ String | nil (private)

Inverts and stringifies a boolean.

Parameters:

  • value (true | false | nil) —

    The boolean.

Returns:

  • (String | nil) —

    The string.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/uri/options_mapper.rb', line 454

def stringify_inverse_bool(value)
  revert_inverse_bool(value)&.to_s
end

#stringify_max_staleness(value) ⇒ String | nil (private)

Stringifies max staleness.

Parameters:

  • value (Integer | nil) —

    The max staleness.

Returns:

  • (String | nil) —

    The string.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/uri/options_mapper.rb', line 712

def stringify_max_staleness(value)
  revert_max_staleness(value)&.to_s
end

#stringify_ms(value) ⇒ String (private)

Stringifies an ms.

Parameters:

  • value (Float) —

    The float.

Returns:

  • (String) —

    The string.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/uri/options_mapper.rb', line 540

def stringify_ms(value)
  revert_ms(value).to_s
end

#stringify_read_mode(value) (private)

Alias for #revert_read_mode.

[ GitHub ]

  
# File 'lib/mongo/uri/options_mapper.rb', line 734

alias stringify_read_mode revert_read_mode

#stringify_read_tags(value) ⇒ String | nil (private)

Stringifies read tags.

Parameters:

  • value (Array<Hash> | nil) —

    The read tags.

Returns:

  • (String | nil) —

    The joined string of read tags.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/uri/options_mapper.rb', line 788

def stringify_read_tags(value)
  value&.map { |ar| ar.map { |k, v| "#{k}:#{v}" }.join(',') }
end

#stringify_repeated_bool(value) ⇒ Array<true | false> | nil (private)

Stringifies a repeated boolean type.

Parameters:

  • value (Array<true | false> | nil) —

    The repeated boolean.

Returns:

  • (Array<true | false> | nil) —

    The string.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/uri/options_mapper.rb', line 414

def stringify_repeated_bool(value)
  rep = revert_repeated_bool(value)
  if rep.is_a?(Array)
    rep.join(',')
  else
    rep
  end
end

#stringify_server_monitoring_mode(value) ⇒ String (private)

Stringifies server monitoring mode.

Parameters:

  • value (Symbol) —

    The server monitoring mode.

Returns:

  • (String) —

    The server monitoring mode as a string.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/uri/options_mapper.rb', line 757

def stringify_server_monitoring_mode(value)
  value.to_s
end

#stringify_symbol(value) (private)

Alias for #revert_symbol.

[ GitHub ]

  
# File 'lib/mongo/uri/options_mapper.rb', line 562

alias stringify_symbol revert_symbol

#stringify_w(value) ⇒ String (private)

Stringifies write concern.

Parameters:

  • value (Integer | Symbol | String) —

    The write concern.

Returns:

  • (String) —

    The write concern as a string.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/uri/options_mapper.rb', line 842

def stringify_w(value)
  revert_w(value)&.to_s
end

#stringify_zlib_compression_level(value) ⇒ String | nil (private)

Stringifies zlib compression level

Parameters:

  • value (Integer | nil) —

    The write concern.

Returns:

  • (String | nil) —

    The string.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/uri/options_mapper.rb', line 882

def stringify_zlib_compression_level(value)
  revert_zlib_compression_level(value)&.to_s
end

#unsupported_option_message(offset = nil) ⇒ String (private)

Builds a warning about an option the driver does not know. The key itself is not included: it comes from the connection string and may contain text the user intended as a credential.

Parameters:

  • offset (Integer | nil) (defaults to: nil) —

    Position of the option within the connection options segment, if known.

Returns:

  • (String) —

    The warning message.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/uri/options_mapper.rb', line 263

def unsupported_option_message(offset = nil)
  if offset
    "Unsupported option at offset #{offset} in the query segment. It will be ignored."
  else
    'Unsupported option. It will be ignored.'
  end
end