Module: CouchRest::Mixins::DesignDoc::ClassMethods

Defined in:
lib/couchrest/mixins/design_doc.rb

Instance Method Summary collapse

Instance Method Details

#default_design_docObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/couchrest/mixins/design_doc.rb', line 26

def default_design_doc
  {
    "language" => "javascript",
    "views" => {
      'all' => {
        'map' => "function(doc) {
          if (doc['couchrest-type'] == '#{self.to_s}') {
            emit(null,null);
          }
        }"
      }
    }
  }
end

#design_doc_idObject



12
13
14
# File 'lib/couchrest/mixins/design_doc.rb', line 12

def design_doc_id
  "_design/#{design_doc_slug}"
end

#design_doc_slugObject



16
17
18
19
20
21
22
23
24
# File 'lib/couchrest/mixins/design_doc.rb', line 16

def design_doc_slug
  return design_doc_slug_cache if (design_doc_slug_cache && design_doc_fresh)
  funcs = []
  design_doc['views'].each do |name, view|
    funcs << "#{name}/#{view['map']}#{view['reduce']}"
  end
  md5 = Digest::MD5.hexdigest(funcs.sort.join(''))
  self.design_doc_slug_cache = "#{self.to_s}-#{md5}"
end

#refresh_design_docObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/couchrest/mixins/design_doc.rb', line 41

def refresh_design_doc
  did = design_doc_id
  saved = database.get(did) rescue nil
  if saved
    design_doc['views'].each do |name, view|
      saved['views'][name] = view
    end
    database.save_doc(saved)
    self.design_doc = saved
  else
    design_doc['_id'] = did
    design_doc.delete('_rev')
    design_doc.database = database
    design_doc.save
  end
  self.design_doc_fresh = true
end