2009-01-30 03:45:01 +01:00
|
|
|
require 'digest/md5'
|
|
|
|
|
|
|
|
module CouchRest
|
|
|
|
module Mixins
|
|
|
|
module DesignDoc
|
|
|
|
|
|
|
|
def self.included(base)
|
|
|
|
base.extend(ClassMethods)
|
|
|
|
end
|
|
|
|
|
|
|
|
module ClassMethods
|
2009-03-27 21:22:10 +01:00
|
|
|
attr_accessor :design_doc, :design_doc_slug_cache, :design_doc_fresh
|
|
|
|
|
|
|
|
def design_doc
|
|
|
|
@design_doc ||= Design.new(default_design_doc)
|
|
|
|
end
|
|
|
|
|
2009-01-30 03:45:01 +01:00
|
|
|
def design_doc_id
|
|
|
|
"_design/#{design_doc_slug}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def design_doc_slug
|
2009-02-21 00:34:49 +01:00
|
|
|
return design_doc_slug_cache if (design_doc_slug_cache && design_doc_fresh)
|
2009-01-30 03:45:01 +01:00
|
|
|
funcs = []
|
|
|
|
design_doc['views'].each do |name, view|
|
|
|
|
funcs << "#{name}/#{view['map']}#{view['reduce']}"
|
|
|
|
end
|
2009-05-18 04:34:02 +02:00
|
|
|
self.design_doc_slug_cache = self.to_s
|
2009-01-30 03:45:01 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def default_design_doc
|
|
|
|
{
|
|
|
|
"language" => "javascript",
|
|
|
|
"views" => {
|
|
|
|
'all' => {
|
|
|
|
'map' => "function(doc) {
|
|
|
|
if (doc['couchrest-type'] == '#{self.to_s}') {
|
2009-05-24 06:44:44 +02:00
|
|
|
emit(null,1);
|
2009-01-30 03:45:01 +01:00
|
|
|
}
|
2009-05-24 06:44:44 +02:00
|
|
|
}",
|
|
|
|
'reduce' => "function(keys, values) {
|
|
|
|
return sum(values);
|
2009-01-30 03:45:01 +01:00
|
|
|
}"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def refresh_design_doc
|
2009-05-20 20:44:44 +02:00
|
|
|
reset_design_doc
|
|
|
|
save_design_doc
|
2009-03-27 12:27:37 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# Save the design doc onto the default database, and update the
|
|
|
|
# design_doc attribute
|
|
|
|
def save_design_doc
|
2009-05-20 20:44:44 +02:00
|
|
|
reset_design_doc unless design_doc_fresh
|
2009-03-27 12:27:37 +01:00
|
|
|
self.design_doc = update_design_doc(design_doc)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Save the design doc onto a target database in a thread-safe way,
|
|
|
|
# not modifying the model's design_doc
|
|
|
|
def save_design_doc_on(db)
|
|
|
|
update_design_doc(Design.new(design_doc), db)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2009-05-20 20:44:44 +02:00
|
|
|
|
|
|
|
def reset_design_doc
|
2009-05-27 03:27:49 +02:00
|
|
|
current = self.database.get(design_doc_id) rescue nil
|
|
|
|
design_doc['_id'] = design_doc_id
|
|
|
|
if current.nil?
|
|
|
|
design_doc.delete('_rev')
|
|
|
|
else
|
|
|
|
design_doc['_rev'] = current['_rev']
|
|
|
|
end
|
2009-05-20 20:44:44 +02:00
|
|
|
self.design_doc_fresh = true
|
|
|
|
end
|
2009-03-27 12:27:37 +01:00
|
|
|
|
|
|
|
# Writes out a design_doc to a given database, returning the
|
|
|
|
# updated design doc
|
|
|
|
def update_design_doc(design_doc, db = database)
|
|
|
|
saved = db.get(design_doc['_id']) rescue nil
|
2009-01-30 03:45:01 +01:00
|
|
|
if saved
|
|
|
|
design_doc['views'].each do |name, view|
|
|
|
|
saved['views'][name] = view
|
|
|
|
end
|
2009-03-27 12:27:37 +01:00
|
|
|
db.save_doc(saved)
|
|
|
|
saved
|
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
|
|
|
|
|
|
|
|
end # module ClassMethods
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|