Module: Toy::Mongo::AtomicUpdates

Extended by:
ActiveSupport::Concern
Defined in:
lib/toy/mongo/atomic_updates.rb

Instance Method Summary collapse

Instance Method Details

#atomic_update(update, opts = {}) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/toy/mongo/atomic_updates.rb', line 41

def atomic_update(update, opts={})
  options  = {}
  criteria = {:_id => id}
  criteria.update(opts[:criteria]) if opts[:criteria]
  options[:safe] = opts.key?(:safe) ? opts[:safe] : adapter.options[:safe]

  run_callbacks(:save) do
    run_callbacks(:update) do
      adapter.client.update(criteria, update, options)
    end
  end
end

#persistable_changesObject

Very basic method for determining what has changed locally so we can just update changes instead of entire document

Does not work with complex objects (array, hash, set, etc.) as it does not attempt to determine what has changed in them, just whether or not they have changed at all.



18
19
20
21
22
23
24
25
26
27
# File 'lib/toy/mongo/atomic_updates.rb', line 18

def persistable_changes
  attrs = {}
  pattrs = persisted_attributes
  changed.each do |key|
    attribute = self.class.attributes[key.to_s]
    next if attribute.virtual?
    attrs[attribute.persisted_name] = pattrs[attribute.persisted_name]
  end
  attrs
end