module Mongoid::Persistence::Atomic
Public Instance Methods
add_to_set_with_mongoid4(*args)
click to toggle source
# File lib/patches/atomic.rb, line 135 def add_to_set_with_mongoid4(*args) if args.length == 1 && args.first.is_a?(Hash) adds = args.first prepare_atomic_operation do |ops| process_atomic_operations(adds) do |field, value| existing = send(field) || (attributes[field] ||= []) values = [ value ].flatten(1) values.each do |val| existing.push(val) unless existing.include?(val) end ops[atomic_attribute_name(field)] = { "$each" => values } end { "$addToSet" => ops } end else add_to_set_without_mongoid4(*args) end end
bit_with_mongoid4(*args)
click to toggle source
# File lib/patches/atomic.rb, line 155 def bit_with_mongoid4(*args) if args.length == 1 && args.first.is_a?(Hash) operations = args.first prepare_atomic_operation do |ops| process_atomic_operations(operations) do |field, values| value = attributes[field] values.each do |op, val| value = value & val if op.to_s == "and" value = value | val if op.to_s == "or" end attributes[field] = value ops[atomic_attribute_name(field)] = values end { "$bit" => ops } end else bit_without_mongoid4(*args) end end
inc_with_mongoid4(*args)
click to toggle source
# File lib/patches/atomic.rb, line 176 def inc_with_mongoid4(*args) if args.length == 1 && args.first.is_a?(Hash) increments = args.first prepare_atomic_operation do |ops| process_atomic_operations(increments) do |field, value| increment = value.__to_inc__ current = attributes[field] attributes[field] = (current || 0) + increment ops[atomic_attribute_name(field)] = increment end { "$inc" => ops } end else inc_without_mongoid4(*args) end end
pop_with_mongoid4(*args)
click to toggle source
# File lib/patches/atomic.rb, line 194 def pop_with_mongoid4(*args) if args.length == 1 && args.first.is_a?(Hash) pops = args.first prepare_atomic_operation do |ops| process_atomic_operations(pops) do |field, value| values = send(field) value > 0 ? values.pop : values.shift ops[atomic_attribute_name(field)] = value end { "$pop" => ops } end else pop_without_mongoid4(*args) end end
pull_all_with_mongoid4(*args)
click to toggle source
# File lib/patches/atomic.rb, line 227 def pull_all_with_mongoid4(*args) if args.length == 1 && args.first.is_a?(Hash) pulls = args.first prepare_atomic_operation do |ops| process_atomic_operations(pulls) do |field, value| existing = send(field) || [] value.each{ |val| existing.delete(val) } ops[atomic_attribute_name(field)] = value end { "$pullAll" => ops } end else pull_all_without_mongoid4(*args) end end
pull_with_mongoid4(*args)
click to toggle source
# File lib/patches/atomic.rb, line 211 def pull_with_mongoid4(*args) if args.length == 1 && args.first.is_a?(Hash) pulls = args.first prepare_atomic_operation do |ops| process_atomic_operations(pulls) do |field, value| (send(field) || []).delete(value) ops[atomic_attribute_name(field)] = value end { "$pull" => ops } end else pull_without_mongoid4(*args) end end
push_with_mongoid4(*args)
click to toggle source
# File lib/patches/atomic.rb, line 244 def push_with_mongoid4(*args) if args.length == 1 && args.first.is_a?(Hash) pushes = args.first prepare_atomic_operation do |ops| process_atomic_operations(pushes) do |field, value| existing = send(field) || (attributes[field] ||= []) values = [ value ].flatten(1) values.each{ |val| existing.push(val) } ops[atomic_attribute_name(field)] = { "$each" => values } end { "$push" => ops } end else push_without_mongoid4(*args) end end
rename_with_mongoid4(*args)
click to toggle source
# File lib/patches/atomic.rb, line 262 def rename_with_mongoid4(*args) if args.length == 1 && args.first.is_a?(Hash) renames = args.first prepare_atomic_operation do |ops| process_atomic_operations(renames) do |old_field, new_field| new_name = new_field.to_s attributes[new_name] = attributes.delete(old_field) ops[atomic_attribute_name(old_field)] = atomic_attribute_name(new_name) end { "$rename" => ops } end else rename_without_mongoid4(*args) end end
set_with_mongoid4(*args)
click to toggle source
# File lib/patches/atomic.rb, line 279 def set_with_mongoid4(*args) if args.length == 1 && args.first.is_a?(Hash) setters = args.first prepare_atomic_operation do |ops| process_atomic_operations(setters) do |field, value| process_attribute(field.to_s, value) ops[atomic_attribute_name(field)] = attributes[field] end { "$set" => ops } end else set_without_mongoid4(*args) end end
Private Instance Methods
executing_atomically?()
click to toggle source
unset params are consistent, however it returns self in Mongoid
4 def unset_with_mongoid4(*args)
unset_without_mongoid4(*args) self
end alias_method_chain :unset, :mongoid4
# File lib/patches/atomic.rb, line 304 def executing_atomically? !@atomic_updates_to_execute.nil? end
persist_atomic_operations(operations)
click to toggle source
# File lib/patches/atomic.rb, line 342 def persist_atomic_operations(operations) if persisted? selector = atomic_selector _root.collection.find(selector).update(operations) end end
persist_or_delay_atomic_operation(operation)
click to toggle source
# File lib/patches/atomic.rb, line 331 def persist_or_delay_atomic_operation(operation) if executing_atomically? operation.each do |(name, hash)| @atomic_updates_to_execute[name] ||= {} @atomic_updates_to_execute[name].merge!(hash) end else persist_atomic_operations(operation) end end
post_process_persist(result, options = {})
click to toggle source
# File lib/patches/atomic.rb, line 308 def post_process_persist(result, options = {}) post_persist unless result == false errors.clear unless performing_validations?(options) true end
prepare_atomic_operation() { |{}| ... }
click to toggle source
# File lib/patches/atomic.rb, line 314 def prepare_atomic_operation operations = yield({}) persist_or_delay_atomic_operation(operations) self end
process_atomic_operations(operations) { |normalized, value| ... }
click to toggle source
# File lib/patches/atomic.rb, line 320 def process_atomic_operations(operations) operations.each do |field, value| unless attribute_writable?(field) raise Errors::ReadonlyAttribute.new(field, value) end normalized = database_field_name(field) yield(normalized, value) remove_change(normalized) end end