 global = @
 
 Collections =
+
+####################################################################################################
+# COLLECTIONS
+####################################################################################################
 
   allow: -> true
   allowAll: -> insert: @allow, update: @allow, remove: @allow

            delete idMap[id]
    Q.all(insertPromises).then -> dest

  # @param {Array.<Meteor.Collection>} collections
  # @returns {Object.<String, Meteor.Collection>} A map of collection name to object for the given
  # collections.
  getMap: (collections) ->
    collectionMap = {}
    _.each collections, (collection) =>
      name = @getName(collection)
      collectionMap[name] = collection
    collectionMap

  # @param {Object.<String, String>} map - A map of IDs to names of the items.
  # @returns A temporary collection with items created from the given map.
  fromNameMap: (map, args) ->
    args = _.extend({
    }, args)
    collection = Collections.createTemporary()
    callback = args.callback
    _.each map, (item, id) ->
      if callback
        name = callback(item, id)
      else
        name = item
      collection.insert(_id: id, name: name)
    collection

  # @returns {String} Generates a MongoDB ObjectID hex string.
  generateId: -> new Mongo.ObjectID().toHexString()

  # @param {Object} doc
  # @param {Object} modifier - A MongoDB modifier object.

    else
      handle(result)

####################################################################################################
# SANITIZATION
####################################################################################################

  # @param {Meteor.Collection} collection
  # @param {Function} sanitize - A sanitization method which is passed a document before insertions
  #      or updates occur for the given collection. If a MongoDB modifier is returned, it is applied
  #      to the document before the operation takes place, allowing for any final changes.
  addSanitization: (collection, sanitize) ->
    collection.before.insert (userId, doc) =>
      context = {userId: userId}
      modifier = sanitize.call(context, doc)
      if modifier
        # TODO(aramk) Apply the change directly on the doc for better performance.
        updatedDoc = @simulateModifierUpdate(doc, modifier)
        Setter.merge(doc, updatedDoc)

    collection.before.update (userId, doc, fieldNames, modifier) ->
      updatedDoc = Collections.simulateModifierUpdate(doc, modifier)
      context = {userId: userId}
      # sanitizedDoc = Setter.clone(updatedDoc)
      sanitizeModifier = sanitize.call(context, updatedDoc)
      return unless sanitizeModifier
      # docDiff = Objects.diff(updatedDoc, sanitizedDoc)
      Setter.merge(modifier, sanitizeModifier)
      # Ensure no fields exist in $unset from $set.
      $unset = modifier.$unset
      if $unset
        _.each $set, (value, key) ->
          delete $unset[key]
