Class: Mongo::URI::OptionsMapper Private

Inherits:
Object
  • Object
show all
Includes:
Loggable
Defined in:
lib/mongo/uri/options_mapper.rb

Overview

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

Performs mapping between URI options and Ruby options.

This class contains:

  • The mapping defining how URI options are converted to Ruby options.

  • The mapping from downcased URI option names to canonical-cased URI option names.

  • Methods to perform conversion of 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.

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

Since:

  • 2.0.0

Constant Summary

Constants included from Loggable

Loggable::PREFIX

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Loggable

#log_debug, #log_error, #log_fatal, #log_info, #log_warn, #logger

Constructor Details

#initialize(**opts) ⇒ OptionsMapper

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.

Instantates 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



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

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

Instance Attribute Details

#optionsHash (readonly)

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.

Returns The options.

Returns:

  • (Hash)

    The options.

Since:

  • 2.0.0



51
52
53
# File 'lib/mongo/uri/options_mapper.rb', line 51

def options
  @options
end

Instance Method Details

#add_uri_option(key, value, uri_options) ⇒ 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.

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)

    URI option name.

  • value (String)

    The value of the option.

  • uri_options (Hash)

    The base option target.

Since:

  • 2.0.0



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/mongo/uri/options_mapper.rb', line 62

def add_uri_option(key, value, uri_options)
  strategy = URI_OPTION_MAP[key.downcase]
  if strategy.nil?
    log_warn("Unsupported URI option '#{key}' on URI '#{@string}'. It will be ignored.")
    return
  end

  group = strategy[:group]
  target = if group
    uri_options[group] || {}
  else
    uri_options
  end
  value = apply_transform(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.
  unless value.nil?
    merge_uri_option(target, value, strategy[:name])
  end

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

#ruby_to_smc(opts) ⇒ Hash

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.

Converts Ruby options provided to “standardized MongoClient options”.

Parameters:

  • opts (Hash)

    Ruby options to convert.

Returns:

  • (Hash)

    Standardized MongoClient options.

Since:

  • 2.0.0



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/mongo/uri/options_mapper.rb', line 126

def ruby_to_smc(opts)
  rv = {}
  URI_OPTION_MAP.each do |uri_key, spec|
    if spec[:group]
      v = opts[spec[:group]]
      v = v && v[spec[:name]]
    else
      v = opts[spec[:name]]
    end
    unless v.nil?
      if type = spec[:type]
        v = send("revert_#{type}", v)
      end
      canonical_key = URI_OPTION_CANONICAL_NAMES[uri_key]
      unless canonical_key
        raise ArgumentError, "Option #{uri_key} is not known"
      end
      rv[canonical_key] = v
    end
  end
  # For options that default to true, remove the value if it is true.
  %w(retryReads retryWrites).each do |k|
    if rv[k]
      rv.delete(k)
    end
  end
  # Remove auth source when it is $external for mechanisms that default
  # (or require) that auth source.
  if %w(MONGODB-AWS).include?(rv['authMechanism']) && rv['authSource'] == '$external'
    rv.delete('authSource')
  end
  # 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

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.

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

Parameters:

  • opts (Hash)

    Ruby options to convert.

Returns:

  • (Hash)

    URI string hash.

Since:

  • 2.0.0



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/mongo/uri/options_mapper.rb', line 169

def ruby_to_string(opts)
  rv = {}
  URI_OPTION_MAP.each do |uri_key, spec|
    if spec[:group]
      v = opts[spec[:group]]
      v = v && v[spec[:name]]
    else
      v = opts[spec[:name]]
    end
    unless v.nil?
      if type = spec[:type]
        v = send("stringify_#{type}", v)
      end
      canonical_key = URI_OPTION_CANONICAL_NAMES[uri_key]
      unless canonical_key
        raise ArgumentError, "Option #{uri_key} is not known"
      end
      rv[canonical_key] = v
    end
  end
  # For options that default to true, remove the value if it is true.
  %w(retryReads retryWrites).each do |k|
    if rv[k]
      rv.delete(k)
    end
  end
  # Remove auth source when it is $external for mechanisms that default
  # (or require) that auth source.
  if %w(MONGODB-AWS).include?(rv['authMechanism']) && rv['authSource'] == '$external'
    rv.delete('authSource')
  end
  # 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) ⇒ 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.

Since:

  • 2.0.0



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/mongo/uri/options_mapper.rb', line 88

def smc_to_ruby(opts)
  uri_options = {}

  opts.each do |key, value|
    strategy = URI_OPTION_MAP[key.downcase]
    if strategy.nil?
      log_warn("Unsupported URI option '#{key}' on URI '#{@string}'. It will be ignored.")
      return
    end

    group = strategy[:group]
    target = if group
      uri_options[group] || {}
    else
      uri_options
    end

    value = apply_transform(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.
    unless value.nil?
      merge_uri_option(target, value, strategy[:name])
    end

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

  uri_options
end