2011-04-13 15:42:28 +02:00
|
|
|
|
|
|
|
CouchRest::Design.class_eval do
|
|
|
|
|
2011-04-17 02:46:33 +02:00
|
|
|
# Calculate and update the checksum of the Design document.
|
|
|
|
# Used for ensuring the latest version has been sent to the database.
|
2011-04-13 15:42:28 +02:00
|
|
|
#
|
|
|
|
# This will generate an flatterned, ordered array of all the elements of the
|
|
|
|
# design document, convert to string then generate an MD5 Hash. This should
|
|
|
|
# result in a consisitent Hash accross all platforms.
|
|
|
|
#
|
2011-04-17 02:46:33 +02:00
|
|
|
def checksum!
|
2011-04-13 15:42:28 +02:00
|
|
|
# create a copy of basic elements
|
|
|
|
base = self.dup
|
|
|
|
base.delete('_id')
|
|
|
|
base.delete('_rev')
|
2011-04-17 02:46:33 +02:00
|
|
|
base.delete('couchrest-hash')
|
2011-04-13 15:42:28 +02:00
|
|
|
result = nil
|
|
|
|
flatten =
|
2011-04-14 00:19:10 +02:00
|
|
|
lambda {|r|
|
|
|
|
(recurse = lambda {|v|
|
2011-06-08 18:22:35 +02:00
|
|
|
if v.is_a?(Hash) || v.is_a?(CouchRest::Document)
|
2011-04-14 00:19:10 +02:00
|
|
|
v.to_a.map{|v| recurse.call(v)}.flatten
|
|
|
|
elsif v.is_a?(Array)
|
|
|
|
v.flatten.map{|v| recurse.call(v)}
|
|
|
|
else
|
|
|
|
v.to_s
|
|
|
|
end
|
|
|
|
}).call(r)
|
2011-04-13 15:42:28 +02:00
|
|
|
}
|
2011-04-17 02:46:33 +02:00
|
|
|
self['couchrest-hash'] = Digest::MD5.hexdigest(flatten.call(base).sort.join(''))
|
2011-04-13 15:42:28 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|