Class: CouchPotato::View::ModelViewSpec

Inherits:
BaseViewSpec show all
Defined in:
lib/couch_potato/view/model_view_spec.rb

Overview

A view to return model instances by searching its properties. If you pass reduce => true will count instead

example:

view :my_view, :key => :name

Direct Known Subclasses

PropertiesViewSpec

Instance Attribute Summary

Attributes inherited from BaseViewSpec

#design_document, #view_name

Instance Method Summary collapse

Methods inherited from BaseViewSpec

#initialize

Constructor Details

This class inherits a constructor from CouchPotato::View::BaseViewSpec

Instance Method Details

#map_functionObject



19
20
21
22
23
24
25
# File 'lib/couch_potato/view/model_view_spec.rb', line 19

def map_function
  "function(doc) {
     if(doc.#{JSON.create_id} && doc.#{JSON.create_id} == '#{@klass.name}') {
       emit(#{formatted_key(key)}, null);
     }
   }"
end

#process_results(results) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/couch_potato/view/model_view_spec.rb', line 33

def process_results(results)
  if count?
    results['rows'].first.try(:[], 'value') || 0
  else
    results['rows'].map { |row| row['doc'] }
  end
end

#reduce_functionObject



27
28
29
30
31
# File 'lib/couch_potato/view/model_view_spec.rb', line 27

def reduce_function
  "function(key, values) {
    return values.length;
  }"
end

#view_parametersObject



10
11
12
13
14
15
16
17
# File 'lib/couch_potato/view/model_view_spec.rb', line 10

def view_parameters
  _super = super
  if _super[:reduce]
    _super
  else
    {:include_docs => true, :reduce => false}.merge(_super)
  end
end