2010-06-20 22:01:11 +02:00
|
|
|
# encoding: utf-8
|
2009-01-30 03:45:01 +01:00
|
|
|
module CouchRest
|
2010-06-20 22:01:11 +02:00
|
|
|
module Model
|
2009-01-30 03:45:01 +01:00
|
|
|
module DesignDoc
|
|
|
|
|
|
|
|
def self.included(base)
|
|
|
|
base.extend(ClassMethods)
|
|
|
|
end
|
|
|
|
|
|
|
|
module ClassMethods
|
2009-03-27 21:22:10 +01:00
|
|
|
|
|
|
|
def design_doc
|
|
|
|
@design_doc ||= Design.new(default_design_doc)
|
|
|
|
end
|
2010-04-06 19:51:17 +02:00
|
|
|
|
|
|
|
# Use when something has been changed, like a view, so that on the next request
|
2010-04-07 23:00:51 +02:00
|
|
|
# the design docs will be updated (if changed!)
|
2010-04-06 19:51:17 +02:00
|
|
|
def req_design_doc_refresh
|
|
|
|
@design_doc_fresh = { }
|
|
|
|
end
|
2009-03-27 21:22:10 +01:00
|
|
|
|
2009-01-30 03:45:01 +01:00
|
|
|
def design_doc_id
|
|
|
|
"_design/#{design_doc_slug}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def design_doc_slug
|
2010-04-06 19:51:17 +02:00
|
|
|
self.to_s
|
2009-01-30 03:45:01 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def default_design_doc
|
|
|
|
{
|
2010-04-16 02:51:59 +02:00
|
|
|
"_id" => design_doc_id,
|
2009-01-30 03:45:01 +01:00
|
|
|
"language" => "javascript",
|
|
|
|
"views" => {
|
|
|
|
'all' => {
|
|
|
|
'map' => "function(doc) {
|
|
|
|
if (doc['couchrest-type'] == '#{self.to_s}') {
|
2009-07-23 00:45:18 +02:00
|
|
|
emit(doc['_id'],1);
|
2009-01-30 03:45:01 +01:00
|
|
|
}
|
|
|
|
}"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2010-04-07 23:00:51 +02:00
|
|
|
# DEPRECATED
|
|
|
|
# use stored_design_doc to retrieve the current design doc
|
|
|
|
def all_design_doc_versions(db = database)
|
|
|
|
db.documents :startkey => "_design/#{self.to_s}",
|
|
|
|
:endkey => "_design/#{self.to_s}-\u9999"
|
|
|
|
end
|
|
|
|
|
|
|
|
# Retreive the latest version of the design document directly
|
|
|
|
# from the database.
|
|
|
|
def stored_design_doc(db = database)
|
|
|
|
db.get(design_doc_id) rescue nil
|
|
|
|
end
|
|
|
|
alias :model_design_doc :stored_design_doc
|
|
|
|
|
2010-04-06 19:51:17 +02:00
|
|
|
def refresh_design_doc(db = database)
|
2010-04-07 23:00:51 +02:00
|
|
|
raise "Database missing for design document refresh" if db.nil?
|
2010-04-06 19:51:17 +02:00
|
|
|
unless design_doc_fresh(db)
|
|
|
|
save_design_doc(db)
|
2010-04-07 23:00:51 +02:00
|
|
|
design_doc_fresh(db, true)
|
2010-04-06 19:51:17 +02:00
|
|
|
end
|
2009-03-27 12:27:37 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# Save the design doc onto a target database in a thread-safe way,
|
|
|
|
# not modifying the model's design_doc
|
2010-04-07 23:00:51 +02:00
|
|
|
#
|
|
|
|
# See also save_design_doc! to always save the design doc even if there
|
|
|
|
# are no changes.
|
|
|
|
def save_design_doc(db = database, force = false)
|
|
|
|
update_design_doc(Design.new(design_doc), db, force)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Force the update of the model's design_doc even if it hasn't changed.
|
|
|
|
def save_design_doc!(db = database)
|
|
|
|
save_design_doc(db, true)
|
2009-03-27 12:27:37 +01:00
|
|
|
end
|
|
|
|
|
2010-04-06 19:51:17 +02:00
|
|
|
protected
|
|
|
|
|
|
|
|
def design_doc_fresh(db, fresh = nil)
|
|
|
|
@design_doc_fresh ||= {}
|
|
|
|
if fresh.nil?
|
|
|
|
@design_doc_fresh[db.uri] || false
|
|
|
|
else
|
|
|
|
@design_doc_fresh[db.uri] = fresh
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-03-27 12:27:37 +01:00
|
|
|
# Writes out a design_doc to a given database, returning the
|
|
|
|
# updated design doc
|
2010-04-07 23:00:51 +02:00
|
|
|
def update_design_doc(design_doc, db, force = false)
|
2010-04-14 22:47:26 +02:00
|
|
|
saved = stored_design_doc(db)
|
2009-01-30 03:45:01 +01:00
|
|
|
if saved
|
2010-04-16 02:51:59 +02:00
|
|
|
changes = force
|
|
|
|
design_doc['views'].each do |name, view|
|
2010-06-15 02:10:14 +02:00
|
|
|
if !compare_views(saved['views'][name], view)
|
2010-04-16 02:51:59 +02:00
|
|
|
changes = true
|
2010-04-07 23:00:51 +02:00
|
|
|
saved['views'][name] = view
|
|
|
|
end
|
2010-04-16 02:51:59 +02:00
|
|
|
end
|
|
|
|
if changes
|
2010-04-07 23:00:51 +02:00
|
|
|
db.save_doc(saved)
|
2009-01-30 03:45:01 +01:00
|
|
|
end
|
2010-04-16 02:51:59 +02:00
|
|
|
design_doc
|
2009-01-30 03:45:01 +01:00
|
|
|
else
|
2009-03-27 12:27:37 +01:00
|
|
|
design_doc.database = db
|
2009-01-30 03:45:01 +01:00
|
|
|
design_doc.save
|
2009-03-27 12:27:37 +01:00
|
|
|
design_doc
|
2009-01-30 03:45:01 +01:00
|
|
|
end
|
|
|
|
end
|
2010-04-07 23:00:51 +02:00
|
|
|
|
2010-06-15 02:10:14 +02:00
|
|
|
# Return true if the two views match
|
|
|
|
def compare_views(orig, repl)
|
|
|
|
return false if orig.nil? or repl.nil?
|
|
|
|
(orig['map'].to_s.strip == repl['map'].to_s.strip) && (orig['reduce'].to_s.strip == repl['reduce'].to_s.strip)
|
|
|
|
end
|
|
|
|
|
2009-01-30 03:45:01 +01:00
|
|
|
end # module ClassMethods
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
2010-04-06 19:51:17 +02:00
|
|
|
end
|