module Mongoid::Positional
def positionally(selector, operations, processed = {})
-
(Hash)- The new operations.
Parameters:
-
processed(Hash) -- The processed update operations. -
operations(Hash) -- The update operations. -
selector(Hash) -- The selector.
Other tags:
- Example: Process the operations. -
Other tags:
- Note: - The only time we can accurately know when to use the positional
def positionally(selector, operations, processed = {}) if selector.size == 1 || selector.values.any? { |val| val.nil? } return operations end keys = selector.keys.map{ |m| m.sub('._id','') } - ['_id'] keys = keys.sort_by { |s| s.length*-1 } process_operations(keys, operations, processed) end
def process_operations(keys, operations, processed)
def process_operations(keys, operations, processed) operations.each_pair do |operation, update| processed[operation] = process_updates(keys, update) end processed end
def process_updates(keys, update, updates = {})
def process_updates(keys, update, updates = {}) update.each_pair do |position, value| updates[replace_index(keys, position)] = value end updates end
def replace_index(keys, position)
def replace_index(keys, position) # replace index with $ only if that key is in the selector and it is only # nested a single level deep. matches = position.scan(/\.\d+\./) if matches.size == 1 keys.each do |kk| if position =~ /\A#{kk}\.\d+\.(.*)\z/ return "#{kk}.$.#{$1}" end end end position end