Module: Mongo::Options::Mapper
Relationships & Source Files | |
Defined in: | lib/mongo/options/mapper.rb |
Overview
Utility class for various options mapping behavior.
Instance Method Summary
-
#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_keys_to_symbols(options) ⇒ Hash
Coverts all the keys of the options to symbols.
-
#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.
::Mongo::Options
which are not present in the provided mapping are returned unmodified.
# File 'lib/mongo/options/mapper.rb', line 42
def transform(, mappings) map = transform_keys_to_strings(mappings) opts = transform_keys_to_strings( ) opts.reduce({}) do |transformed, (key, value)| if map[key] transformed[map[key]] = value else transformed[key] = value end 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.
# File 'lib/mongo/options/mapper.rb', line 69
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.
# File 'lib/mongo/options/mapper.rb', line 87
def transform_keys_to_strings( ) .reduce({}) do |transformed, (key, value)| transformed[key.to_s] = value transformed end end
#transform_keys_to_symbols(options) ⇒ Hash
Coverts all the keys of the options to symbols.
# File 'lib/mongo/options/mapper.rb', line 104
def transform_keys_to_symbols( ) .reduce({}) do |transformed, (key, value)| transformed[key.to_sym] = value transformed end end
#transform_values_to_strings(options) ⇒ Hash
Coverts all the symbol values to strings.
# File 'lib/mongo/options/mapper.rb', line 121
def transform_values_to_strings( ) .reduce({}) do |transformed, (key, value)| transformed[key] = value.is_a?(Symbol) ? value.to_s : value transformed end end