Module: CouchFoo::ViewMethods::ClassMethods

Defined in:
lib/couch_foo/view_methods.rb

Instance Method Summary collapse

Instance Method Details

#count_view(options) ⇒ Object

Find the number of documents in a view. If the CouchDB version is greater than 0.8 then we use the same view as the finder so we only need to keep one view for both finding and counting on the same attributes



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/couch_foo/view_methods.rb', line 31

def count_view(options)
  search_fields = search_fields(options)
  
  if database.version > 0.8
    view_name = get_view_name(search_fields)
  else
    view_name = get_view_name(search_fields, "count")
  end
  
  options[:return_json] = true
  result = generic_view(view_name, find_by_function(search_fields), count_documents_function, options)
  
  result['rows'].first['value'] rescue 0
end

#find_view(options) ⇒ Object

Method that does find_by_id, find_by_username_and_type views. If the CouchDB version is greater than 0.8 it will add the counter function into the same design document but add an option to not perform it. This makes any calls to count the number of records more efficient in future as there’s only one index to keep updated



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/couch_foo/view_methods.rb', line 16

def find_view(options)
  search_fields = search_fields(options)
  
  reduce_function = nil
  if database.version > 0.8
    reduce_function = count_documents_function
    options[:reduce] = false
  end
  
  generic_view(get_view_name(search_fields), find_by_function(search_fields), reduce_function, options)
end

#generic_view(view_name, find_function, reduce_function = nil, options = {}) ⇒ Object

Perform a view operation with passed name, functions and options



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/couch_foo/view_methods.rb', line 47

def generic_view(view_name, find_function, reduce_function = nil, options = {})
  return_json = options.delete(:return_json)
  order = options.delete(:order) || default_sort_order
  readonly = options.delete(:readonly)
  
  if options.delete(:view_type) == :slow
    result = query_slow_view(find_function, reduce_function, options)
  else
    result = query_view(view_name, find_function, reduce_function, options)
  end
  
  if return_json
    result
  else
    instantiate_instances(result, readonly, order)
  end
end