Module: Mongo::Options::Mapper
Overview
Utility class for various options mapping behavior.
Instance Method Summary collapse
-
#transform(options, mappings) ⇒ Hash
Transforms the provided options to a new set of options given the provided mapping.
-
#transform_documents(options, mappings, document = BSON::Document.new) ⇒ BSON::Document
Transforms the provided options to a new set of options given the provided mapping.
-
#transform_keys_to_strings(options) ⇒ Hash
Converts all the keys of the options to strings.
-
#transform_keys_to_symbols(options) ⇒ Hash
Converts all the keys of the options to symbols.
-
#transform_values_to_strings(options) ⇒ Hash
Converts all the symbol values to strings.
Instance Method Details
#transform(options, mappings) ⇒ Hash
Transforms the provided options to a new set of options given the provided mapping.
Options which are not present in the provided mapping are returned unmodified.
40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/mongo/options/mapper.rb', line 40 def transform(, mappings) map = transform_keys_to_strings(mappings) opts = transform_keys_to_strings() opts.each_with_object({}) do |(key, value), transformed| if map[key] transformed[map[key]] = value else transformed[key] = value end end end |
#transform_documents(options, mappings, document = BSON::Document.new) ⇒ BSON::Document
Transforms the provided options to a new set of options given the provided mapping. Expects BSON::Documents in and out so no explicit string conversion needs to happen.
66 67 68 69 70 71 |
# File 'lib/mongo/options/mapper.rb', line 66 def transform_documents(, mappings, document = BSON::Document.new) .each_with_object(document) do |(key, value), transformed| name = mappings[key] transformed[name] = value if name && !value.nil? end end |
#transform_keys_to_strings(options) ⇒ Hash
Converts all the keys of the options to strings.
83 84 85 86 87 |
# File 'lib/mongo/options/mapper.rb', line 83 def transform_keys_to_strings() .each_with_object({}) do |(key, value), transformed| transformed[key.to_s] = value end end |
#transform_keys_to_symbols(options) ⇒ Hash
Converts all the keys of the options to symbols.
99 100 101 102 103 |
# File 'lib/mongo/options/mapper.rb', line 99 def transform_keys_to_symbols() .each_with_object({}) do |(key, value), transformed| transformed[key.to_sym] = value end end |
#transform_values_to_strings(options) ⇒ Hash
Converts all the symbol values to strings.
115 116 117 118 119 |
# File 'lib/mongo/options/mapper.rb', line 115 def transform_values_to_strings() .each_with_object({}) do |(key, value), transformed| transformed[key] = value.is_a?(Symbol) ? value.to_s : value end end |