class CouchRest::Model::Designs::List

Attributes

view_name[RW]

Public Class Methods

create(model, name, function) click to toggle source

Simplified list creation. A new list will be added to the provided model's design document using the name and options.

If the view name starts with “by_” and :by is not provided in the options, the new list's map method will be interpreted and generated automatically. For example:

List.create(Meeting, "by_date_and_name")

Will create a list that searches by the date and name properties. Explicity setting the attributes to use is possible using the :by option. For example:

List.create(Meeting, "by_date_and_name", :by => [:date, :firstname, :lastname])
# File lib/couchrest/extensions/list.rb, line 140
def create(model, name, function)            
  model.design_doc['lists'] ||= {}
  list = model.design_doc['lists'][name.to_s] = function
  list
end
new(parent, view_name, new_query = {}, name = nil) click to toggle source
Calls superclass method
# File lib/couchrest/extensions/list.rb, line 87
def initialize(parent, view_name, new_query = {}, name = nil)
  self.view_name = view_name
  super(parent, new_query, name)
end

Public Instance Methods

execute() click to toggle source
# File lib/couchrest/extensions/list.rb, line 108
def execute
  return self.result if result
  raise "Database must be defined in model or list!" if use_database.nil?

  # Remove the reduce value if its not needed to prevent CouchDB errors
  #query.delete(:reduce) unless can_reduce?
  
  if model.send(view_name.to_sym).can_reduce?          
    query[:reduce] = false if query[:include_docs] # don't reduce if we include_docs
  end

  model.save_design_doc(use_database)

  self.result = model.design_doc.list_on(use_database, name, view_name, query.reject{|k,v| v.nil?})
end
rows() click to toggle source

Return each row wrapped in a ViewRow object. Unlike the raw CouchDB request, this will provide an empty array if there are no results.

# File lib/couchrest/extensions/list.rb, line 99
def rows
  return @rows if @rows
  if execute && result['rows']
    @rows ||= result['rows'].map{|v| ViewRow.new(v, model)}
  else 
    [ ]
  end
end