Class: Mongo::URI::OptionsMapper Private
- Inherits:
-
Object
- Object
- Mongo::URI::OptionsMapper
- 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).
Constant Summary collapse
- URI_OPTION_MAP =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Hash for storing map of URI option parameters to conversion strategies
{}
- URI_OPTION_CANONICAL_NAMES =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Returns Map from lowercased to canonical URI option names.
{}
Constants included from Loggable
Instance Attribute Summary collapse
-
#options ⇒ Hash
readonly
private
The options.
Class Method Summary collapse
-
.uri_option(uri_key, name, **extra) ⇒ Object
private
Simple internal dsl to register a MongoDB URI option in the URI_OPTION_MAP.
Instance Method Summary collapse
-
#add_uri_option(key, value, uri_options) ⇒ Object
private
Adds an option to the uri options hash.
-
#initialize(**opts) ⇒ OptionsMapper
constructor
private
Instantiates the options mapper.
-
#ruby_to_smc(opts) ⇒ Hash
private
Converts Ruby options provided to “standardized MongoClient options”.
-
#ruby_to_string(opts) ⇒ Hash
private
Converts Ruby options provided to their representation in a URI string.
- #smc_to_ruby(opts) ⇒ Object private
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.
Instantiates the options mapper.
43 44 45 |
# File 'lib/mongo/uri/options_mapper.rb', line 43 def initialize(**opts) @options = opts end |
Instance Attribute Details
#options ⇒ Hash (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.
48 49 50 |
# File 'lib/mongo/uri/options_mapper.rb', line 48 def @options end |
Class Method Details
.uri_option(uri_key, name, **extra) ⇒ 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.
Simple internal dsl to register a MongoDB URI option in the URI_OPTION_MAP.
205 206 207 208 |
# File 'lib/mongo/uri/options_mapper.rb', line 205 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 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.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/mongo/uri/options_mapper.rb', line 59 def add_uri_option(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 [group] || {} else 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. merge_uri_option(target, value, strategy[:name]) unless value.nil? return unless group && !target.empty? && !.key?(group) [group] = target 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”.
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/mongo/uri/options_mapper.rb', line 117 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
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.
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/mongo/uri/options_mapper.rb', line 155 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) ⇒ 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.
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/mongo/uri/options_mapper.rb', line 83 def smc_to_ruby(opts) = {} 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 [group] || {} else 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. merge_uri_option(target, value, strategy[:name]) unless value.nil? [group] = target if group && !target.empty? && !.key?(group) end end |