Class: CouchRest::Model::Designs::List

Inherits:
View
  • Object
show all
Defined in:
lib/couchrest/extensions/list.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, view_name, new_query = {}, name = nil) ⇒ List

Returns a new instance of List.



87
88
89
90
# 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

Instance Attribute Details

#view_nameObject

Returns the value of attribute view_name.



85
86
87
# File 'lib/couchrest/extensions/list.rb', line 85

def view_name
  @view_name
end

Class Method Details

.create(model, name, function) ⇒ Object

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])


140
141
142
143
144
# 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

Instance Method Details

#executeObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# 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

#rowsObject

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



99
100
101
102
103
104
105
106
# 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