module MarkMapper::Plugins::Modifiers::ClassMethods

Public Instance Methods

add_to_set(*args) click to toggle source
# File lib/mark_mapper/plugins/modifiers.rb, line 50
def add_to_set(*args)
  modifier_update('$addToSet', args)
end
Also aliased as: push_uniq
decrement(*args) click to toggle source
# File lib/mark_mapper/plugins/modifiers.rb, line 12
def decrement(*args)
  criteria, keys, options = criteria_and_keys_from_args(args)
  values, to_decrement = keys.values, {}
  keys.keys.each_with_index { |k, i| to_decrement[k] = -values[i].abs }
  collection.update(criteria, {'$inc' => to_decrement})
end
find_and_modify(args) click to toggle source
# File lib/mark_mapper/plugins/modifiers.rb, line 67
def find_and_modify(args)
  args[:query]  = dealias_keys(args[:query])  if args.key? :query
  args[:update] = dealias_keys(args[:update]) if args.key? :update
  collection.find_and_modify(args)
end
increment(*args) click to toggle source
# File lib/mark_mapper/plugins/modifiers.rb, line 8
def increment(*args)
  modifier_update('$inc', args)
end
pop(*args) click to toggle source
# File lib/mark_mapper/plugins/modifiers.rb, line 63
def pop(*args)
  modifier_update('$pop', args)
end
pull(*args) click to toggle source
# File lib/mark_mapper/plugins/modifiers.rb, line 55
def pull(*args)
  modifier_update('$pull', args)
end
pull_all(*args) click to toggle source
# File lib/mark_mapper/plugins/modifiers.rb, line 59
def pull_all(*args)
  modifier_update('$pullAll', args)
end
push(*args) click to toggle source
# File lib/mark_mapper/plugins/modifiers.rb, line 42
def push(*args)
  modifier_update('$push', args)
end
push_all(*args) click to toggle source
# File lib/mark_mapper/plugins/modifiers.rb, line 46
def push_all(*args)
  modifier_update('$pushAll', args)
end
push_uniq(*args)
Alias for: add_to_set
set(*args) click to toggle source
# File lib/mark_mapper/plugins/modifiers.rb, line 19
def set(*args)
  criteria, updates, options = criteria_and_keys_from_args(args)
  updates.each do |key, value|
    updates[key] = keys[key.to_s].set(value) if key?(key)
  end
  modifier_update('$set', [criteria, updates, options])
end
unset(*args) click to toggle source
# File lib/mark_mapper/plugins/modifiers.rb, line 27
def unset(*args)
  if args[0].is_a?(Hash)
    criteria, keys = args.shift, args
    options = keys.last.is_a?(Hash) ? keys.pop : {}
  else
    keys, ids = args.partition { |arg| arg.is_a?(Symbol) }
    options = ids.last.is_a?(Hash) ? ids.pop : {}
    criteria = {:id => ids}
  end

  criteria = criteria_hash(criteria).to_hash
  updates = keys.inject({}) { |hash, key| hash[key] = 1; hash }
  modifier_update('$unset', [criteria, updates, options])
end

Private Instance Methods

criteria_and_keys_from_args(args) click to toggle source
# File lib/mark_mapper/plugins/modifiers.rb, line 83
def criteria_and_keys_from_args(args)
  if args[0].is_a?(Hash)
    criteria = args[0]
    updates  = args[1]
    options  = args[2]
  else
    criteria, (updates, options) = args.partition { |a| !a.is_a?(Hash) }
    criteria = { :id => criteria }
  end
  updates = dealias_keys updates

  [criteria_hash(criteria).to_hash, updates, options]
end
modifier_update(modifier, args) click to toggle source
# File lib/mark_mapper/plugins/modifiers.rb, line 74
def modifier_update(modifier, args)
  criteria, updates, options = criteria_and_keys_from_args(args)
  if options
    collection.update(criteria, {modifier => updates}, options)
  else
    collection.update(criteria, {modifier => updates})
  end
end