Module: Mongo::Options::Mapper

Extended by:
Mapper
Included in:
Mapper
Defined in:
lib/mongo/options/mapper.rb

Overview

Utility class for various options mapping behavior.

Since:

  • 2.0.0

Instance Method Summary collapse

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.

Examples:

Transform the options.

Mapper.transform({ name: 1 }, { :name => :nombre })

Parameters:

  • options (Hash)

    The options to transform

  • mappings (Hash)

    The key mappings.

Returns:

  • (Hash)

    The transformed options.

Since:

  • 2.0.0



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/mongo/options/mapper.rb', line 42

def transform(options, mappings)
  map = transform_keys_to_strings(mappings)
  opts = transform_keys_to_strings(options)
  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.

Examples:

Transform the options.

Mapper.transform_documents({ name: 1 }, { :name => :nombre })

Parameters:

  • options (BSON::Document)

    The options to transform

  • mappings (BSON::Document)

    The key mappings.

  • document (BSON::Document) (defaults to: BSON::Document.new)

    The output document.

Returns:

  • (BSON::Document)

    The transformed options.

Since:

  • 2.0.0



69
70
71
72
73
74
75
# File 'lib/mongo/options/mapper.rb', line 69

def transform_documents(options, mappings, document = BSON::Document.new)
  options.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.

Examples:

Convert all option keys to strings.

Mapper.transform({ :name => 1 })

Parameters:

  • options (Hash)

    The options to transform.

Returns:

  • (Hash)

    The transformed options.

Since:

  • 2.0.0



87
88
89
90
91
92
# File 'lib/mongo/options/mapper.rb', line 87

def transform_keys_to_strings(options)
  options.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.

Examples:

Convert all option keys to symbols.

Mapper.transform({ 'name' => 1 })

Parameters:

  • options (Hash)

    The options to transform.

Returns:

  • (Hash)

    The transformed options.

Since:

  • 2.2.2



104
105
106
107
108
109
# File 'lib/mongo/options/mapper.rb', line 104

def transform_keys_to_symbols(options)
  options.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.

Examples:

Convert all option symbol values to strings.

Mapper.transform({ :name => 1 })

Parameters:

  • options (Hash)

    The options to transform.

Returns:

  • (Hash)

    The transformed options.

Since:

  • 2.0.0



121
122
123
124
125
126
# File 'lib/mongo/options/mapper.rb', line 121

def transform_values_to_strings(options)
  options.reduce({}) do |transformed, (key, value)|
    transformed[key] = value.is_a?(Symbol) ? value.to_s : value
    transformed
  end
end