Module: Mongo::Options::Mapper
Overview
Utility class for various options mapping behaviour.
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
Coverts all the keys of the options to strings.
-
#transform_values_to_strings(options) ⇒ Hash
Coverts 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.
36 37 38 39 40 41 42 43 |
# File 'lib/mongo/options/mapper.rb', line 36 def transform(, mappings) map = transform_keys_to_strings(mappings) opts = transform_keys_to_strings() opts.reduce({}) do |transformed, (key, value)| transformed[map[key]] = value if map[key] transformed 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.
59 60 61 62 63 64 65 |
# File 'lib/mongo/options/mapper.rb', line 59 def transform_documents(, mappings, document = BSON::Document.new) .reduce(document) do |transformed, (key, value)| name = mappings[key] transformed[name] = value if name && !value.nil? transformed end end |
#transform_keys_to_strings(options) ⇒ Hash
Coverts all the keys of the options to strings.
77 78 79 80 81 82 |
# File 'lib/mongo/options/mapper.rb', line 77 def transform_keys_to_strings() .reduce({}) do |transformed, (key, value)| transformed[key.to_s] = value transformed end end |
#transform_values_to_strings(options) ⇒ Hash
Coverts all the symbol values to strings.
94 95 96 97 98 99 |
# File 'lib/mongo/options/mapper.rb', line 94 def transform_values_to_strings() .reduce({}) do |transformed, (key, value)| transformed[key] = value.is_a?(Symbol) ? value.to_s : value transformed end end |