Module Mongo::Conversions
In: lib/mongo/util/conversions.rb

Utility module to include when needing to convert certain types of objects to mongo-friendly parameters.

Methods

Constants

ASCENDING_CONVERSION = ["ascending", "asc", "1"]
DESCENDING_CONVERSION = ["descending", "desc", "-1"]

Public Instance methods

Converts the supplied Array to a Hash to pass to mongo as sorting parameters. The returned Hash will vary depending on whether the passed Array is one or two dimensional.

Example:

array_as_sort_parameters([["field1", :asc], ["field2", :desc]]) => { "field1" => 1, "field2" => -1}

Allows sort parameters to be defined as a Hash. Does not allow usage of un-ordered hashes, therefore Ruby 1.8.x users must use BSON::OrderedHash.

Example:

hash_as_sort_parameters({:field1 => :asc, "field2" => :desc}) => { "field1" => 1, "field2" => -1}

Converts the String, Symbol, or Integer to the corresponding sort value in MongoDB.

Valid conversions (case-insensitive):

ascending, asc, :ascending, :asc, 1 => 1 descending, desc, :descending, :desc, -1 => -1

If the value is invalid then an error will be raised.

Converts the supplied String or Symbol to a Hash to pass to mongo as a sorting parameter with ascending order. If the String is empty then an empty Hash will be returned.

Example:

*DEPRECATED

string_as_sort_parameters("field") => { "field" => 1 } string_as_sort_parameters("") => {}

[Validate]