Module: Mongoo::Changelog

Included in:
Base
Defined in:
lib/mongoo/changelog.rb

Instance Method Summary collapse

Instance Method Details

#changelogObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/mongoo/changelog.rb', line 4

def changelog
  persisted_mongohash_kv = (self.persisted_mongohash || Mongoo::Mongohash.new).to_key_value
  curr_mongohash_kv      = self.mongohash.to_key_value
  
  log = []
  
  persisted_mongohash_kv.each do |k,v|
    unless curr_mongohash_kv.has_key?(k)
      found = nil
      parts = k.split(".")
      while parts.pop
        if !self.mongohash.dot_get(parts.join("."))
          found = [:unset, parts.join("."), 1]
        end
      end
      found ||= [:unset, k, 1]
      log << found
    end
  end
  
  curr_mongohash_kv.each do |k,v|
    if v != persisted_mongohash_kv[k]
      unless log.include?([:set, k, v])
        log << [:set, k, v]
      end
    end
  end
  
  log.uniq
end