Class: Couch::DocView

Inherits:
View show all
Defined in:
lib/couch/doc_view.rb

Constant Summary

Constants inherited from View

View::DUMMY_NAME

Constants included from Log

Log::QUIET

Instance Attribute Summary

Attributes inherited from Document

#attributes

Class Method Summary collapse

Methods inherited from View

#all, find, #map, #map=, #reduce, #reduce=, #total_rows

Methods inherited from Document

async, #changed?, #database, database, #destroy, #dup, find, #id, #id=, #initialize, #inspect, instantiate, #new_record?, #reload, #rev, #save, #to_hash, #to_json, #type, #update

Methods included from DocExtensions

condition_keys, #count, #view

Constructor Details

This class inherits a constructor from Couch::Document

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Couch::Document

Class Method Details

.create(klass, name, opts = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/couch/doc_view.rb', line 11

def self.create(klass, name, opts = {})
  map = <<-JS
    function(doc) { 
      if(doc.type == #{klass.name.to_json}) { emit(#{key_expression(opts[:keys])}, 1); }
    }
  JS

  reduce = "_sum" if opts[:mode] == :count

  super name, :map => map, :reduce => reduce
end

.find_or_create(klass, opts) ⇒ Object



2
3
4
5
6
7
8
9
# File 'lib/couch/doc_view.rb', line 2

def self.find_or_create(klass, opts)
  name = name_for(klass, opts)

  database.memoized_views[name] ||= begin
    Couch::DocView.find(name) ||
    Couch::DocView.create(klass, name, opts)
  end
end