16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
# File 'lib/mm_no_empties.rb', line 16
def set(*args)
criteria, updates, options = criteria_and_keys_from_args(args)
unset_cmd = {}
updates.each do |key, value|
updates[key] = keys[key.to_s].set(value) if key?(key)
unset_cmd[key] = 1 unless updates[key].present?
end
set_attr = updates.except(*unset_cmd.keys)
update_cmd = {}
update_cmd['$set'] = set_attr if set_attr.present?
update_cmd['$unset'] = unset_cmd if unset_cmd.present?
if options
collection.update(criteria, update_cmd, options.merge(:multi => true))
else
collection.update(criteria, update_cmd, :multi => true)
end
end
|