Module: Mongoid::Patchable
- Defined in:
- lib/mongoid/patchable.rb
Instance Method Summary collapse
-
#apply_patch(patch) ⇒ Object
Apply a patch this this document.
Instance Method Details
#apply_patch(patch) ⇒ Object
Apply a patch this this document.
Currently assumes add_to_set and set operations will succeed.
TODO: is there a better way to ensure atomicity of multiple operations while avoiding race conditions from multiple clients?
it looks like
Preferences.collection.update(=> 1, => {‘follow_suggest_blacklist’ => 4, ‘$set’ => => 2})
would do this, but a) it needs to deal with multiple hunks acting on the same attribute and b) it looks like mongoid doesn’t support this ootb. more research needed…
TODO: handle numeric path segments
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/mongoid/patchable.rb', line 19 def apply_patch(patch) # compile operation information for verification ops = patch.hunks.map do |hunk| (obj, element) = hunk.resolve_path(self) [hunk, obj, element, obj.fields[element.to_s]] end if patch_ops_valid?(ops) # if something goes wrong here, raise an error to let the client # know the patch may be partially applied ops.each_with_index do |(hunk, obj, element, field), index| value = hunk.value case hunk.op when :add process_add(obj, field, element, value) when :replace process_replace(obj, field, element, value) when :remove process_remove(obj, field, element, value) else raise "Illegal operation #{hunk.op} in hunk #{index}" end end true end end |