Working on adding checksum support to design documents to handle updates

This commit is contained in:
Sam Lown 2011-04-13 15:42:28 +02:00
parent a6becd7305
commit 221e5a5470
8 changed files with 89 additions and 41 deletions

View file

@ -0,0 +1,15 @@
CouchRest::Database.class_eval do
alias :delete_orig! :delete!
def delete!
clear_model_fresh_cache
delete_orig!
end
# If the database is deleted, ensure that the design docs will be refreshed.
def clear_model_fresh_cache
::CouchRest::Model::Base.subclasses.each{|klass| klass.req_design_doc_refresh if klass.respond_to?(:req_design_doc_refresh)}
end
end

View file

@ -0,0 +1,24 @@
CouchRest::Design.class_eval do
# Calculate a checksum of the Design document. Used for ensuring the latest
# version has been sent to the database.
#
# 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.
#
def checksum
# create a copy of basic elements
base = self.dup
base.delete('_id')
base.delete('_rev')
result = nil
flatten =
lambda{|v|
v.is_a?(Hash) ? v.flatten.map{|v| flatten.call(v)}.flatten : v
}
Digest::MD5.hexdigest(flatten.call(base).sort.join(''))
end
end