module Mingo::Changes

Attributes

changes[R]

Public Class Methods

included(base) click to toggle source
# File lib/mingo/changes.rb, line 3
def self.included(base)
  base.after_save :clear_changes
end
new(*args) click to toggle source
Calls superclass method
# File lib/mingo/changes.rb, line 9
def initialize(*args)
  @changes = {}
  super
end

Public Instance Methods

[]=(key, value) click to toggle source
Calls superclass method
# File lib/mingo/changes.rb, line 14
def []=(key, value)
  record_change(key, value)
  super
end
changed?() click to toggle source
# File lib/mingo/changes.rb, line 19
def changed?
  changes.any?
end

Private Instance Methods

clear_changes() click to toggle source
# File lib/mingo/changes.rb, line 33
def clear_changes
  changes.clear
end
record_change(key, value) click to toggle source
# File lib/mingo/changes.rb, line 25
def record_change(key, value)
  old_value = self[key]
  unless value == old_value
    memo = (changes[key.to_sym] ||= [old_value])
    memo[0] == value ? changes.delete(key.to_sym) : (memo[1] = value)
  end
end
values_for_update() click to toggle source
# File lib/mingo/changes.rb, line 37
def values_for_update
  changes.inject({}) do |doc, (key, values)|
    value = values[1]
    if value
      doc['$set'] ||= {}
      doc['$set'][key] = value
    else
      doc['$unset'] ||= {}
      doc['$unset'][key] = 1
    end
    doc
  end
end