@@ -5,10 +5,10 @@ module Designs
5
5
#
6
6
# A proxy class that allows view queries to be created using
7
7
# chained method calls. After each call a new instance of the method
8
- # is created based on the original in a similar fashion to ruby's Sequel
8
+ # is created based on the original in a similar fashion to ruby's Sequel
9
9
# library, or Rails 3's Arel.
10
10
#
11
- # CouchDB views have inherent limitations, so joins and filters as used in
11
+ # CouchDB views have inherent limitations, so joins and filters as used in
12
12
# a normal relational database are not possible.
13
13
#
14
14
class View
@@ -52,15 +52,15 @@ def initialize(design_doc, parent, new_query = {}, name = nil)
52
52
# == View Execution Methods
53
53
#
54
54
# Request to the CouchDB database using the current query values.
55
-
55
+
56
56
# Return each row wrapped in a ViewRow object. Unlike the raw
57
57
# CouchDB request, this will provide an empty array if there
58
58
# are no results.
59
59
def rows
60
60
return @rows if @rows
61
61
if execute && result [ 'rows' ]
62
62
@rows ||= result [ 'rows' ] . map { |v | ViewRow . new ( v , model ) }
63
- else
63
+ else
64
64
[ ]
65
65
end
66
66
end
@@ -75,23 +75,23 @@ def all
75
75
end
76
76
77
77
# Provide all the documents from the view. If the view has not been
78
- # prepared with the +include_docs+ option, each document will be
78
+ # prepared with the +include_docs+ option, each document will be
79
79
# loaded individually.
80
80
def docs
81
81
@docs ||= rows . map { |r | r . doc }
82
82
end
83
83
84
- # If another request has been made on the view, this will return
84
+ # If another request has been made on the view, this will return
85
85
# the first document in the set. If not, a new query object will be
86
- # generated with a limit of 1 so that only the first document is
86
+ # generated with a limit of 1 so that only the first document is
87
87
# loaded.
88
88
def first
89
89
result ? all . first : limit ( 1 ) . all . first
90
90
end
91
91
92
92
# Same as first but will order the view in descending order. This
93
- # does not however reverse the search keys or the offset, so if you
94
- # are using a +startkey+ and +endkey+ you might end up with
93
+ # does not however reverse the search keys or the offset, so if you
94
+ # are using a +startkey+ and +endkey+ you might end up with
95
95
# unexpected results.
96
96
#
97
97
# If in doubt, don't use this method!
@@ -115,7 +115,7 @@ def length
115
115
#
116
116
# Trying to use this method with the group option will raise an error.
117
117
#
118
- # If no reduce function is defined, a query will be performed
118
+ # If no reduce function is defined, a query will be performed
119
119
# to return the total number of rows, this is the equivalant of:
120
120
#
121
121
# view.limit(0).total_rows
@@ -130,7 +130,7 @@ def count
130
130
end
131
131
end
132
132
133
- # Check to see if the array of documents is empty. This *will*
133
+ # Check to see if the array of documents is empty. This *will*
134
134
# perform the query and return all documents ready to use, if you don't
135
135
# want to load anything, use +#total_rows+ or +#count+ instead.
136
136
def empty?
@@ -169,7 +169,7 @@ def values
169
169
#
170
170
# In this example, the raw option will be ignored, and the total rows
171
171
# will still be accessible.
172
- #
172
+ #
173
173
def []( value )
174
174
execute [ value ]
175
175
end
@@ -182,8 +182,8 @@ def info
182
182
183
183
184
184
# == View Filter Methods
185
- #
186
- # View filters return a copy of the view instance with the query
185
+ #
186
+ # View filters return a copy of the view instance with the query
187
187
# modified appropriatly. Errors will be raised if the methods
188
188
# are combined in an incorrect fashion.
189
189
#
@@ -196,10 +196,10 @@ def key(value)
196
196
update_query ( :key => value )
197
197
end
198
198
199
- # Find all index keys that start with the value provided. May or may
199
+ # Find all index keys that start with the value provided. May or may
200
200
# not be used in conjunction with the +endkey+ option.
201
201
#
202
- # When the +#descending+ option is used (not the default), the start
202
+ # When the +#descending+ option is used (not the default), the start
203
203
# and end keys should be reversed, as per the CouchDB API.
204
204
#
205
205
# Cannot be used if the key has been set.
@@ -208,7 +208,7 @@ def startkey(value)
208
208
update_query ( :startkey => value )
209
209
end
210
210
211
- # The result set should start from the position of the provided document.
211
+ # The result set should start from the position of the provided document.
212
212
# The value may be provided as an object that responds to the +#id+ call
213
213
# or a string.
214
214
def startkey_doc ( value )
@@ -225,19 +225,19 @@ def endkey(value)
225
225
update_query ( :endkey => value )
226
226
end
227
227
228
- # The result set should end at the position of the provided document.
229
- # The value may be provided as an object that responds to the +#id+
228
+ # The result set should end at the position of the provided document.
229
+ # The value may be provided as an object that responds to the +#id+
230
230
# call or a string.
231
231
def endkey_doc ( value )
232
232
update_query ( :endkey_docid => value . is_a? ( String ) ? value : value . id )
233
233
end
234
234
235
235
# Keys is a special CouchDB option that will cause the view request to be POSTed
236
- # including an array of keys. Only documents with the matching keys will be
237
- # returned. This is much faster than sending multiple requests for a set
236
+ # including an array of keys. Only documents with the matching keys will be
237
+ # returned. This is much faster than sending multiple requests for a set
238
238
# non-consecutive documents.
239
239
#
240
- # If no values are provided, this method will act as a wrapper around
240
+ # If no values are provided, this method will act as a wrapper around
241
241
# the rows result set, providing an array of keys.
242
242
def keys ( *keys )
243
243
if keys . empty?
@@ -290,16 +290,16 @@ def reduce
290
290
# Control whether the reduce function reduces to a set of distinct keys
291
291
# or to a single result row.
292
292
#
293
- # By default the value is false, and can only be set when the view's
293
+ # By default the value is false, and can only be set when the view's
294
294
# +#reduce+ option has been set.
295
295
def group
296
296
raise "View#reduce must have been set before grouping is permitted" unless query [ :reduce ]
297
297
update_query ( :group => true )
298
298
end
299
299
300
- # Will set the level the grouping should be performed to. As per the
300
+ # Will set the level the grouping should be performed to. As per the
301
301
# CouchDB API, it only makes sense when the index key is an array.
302
- #
302
+ #
303
303
# This will automatically set the group option.
304
304
def group_level ( value )
305
305
group . update_query ( :group_level => value . to_i )
@@ -313,7 +313,7 @@ def include_docs
313
313
314
314
# Allow the results of a query to be provided "stale". Setting to 'ok'
315
315
# will disable all view updates for the query.
316
- # When 'update_after' is provided the index will be update after the
316
+ # When 'update_after' is provided the index will be update after the
317
317
# result has been returned.
318
318
def stale ( value )
319
319
unless ( [ 'ok' , 'update_after' ] . include? ( value . to_s ) )
@@ -433,17 +433,17 @@ def define_and_create(design_doc, name, opts = {})
433
433
create_model_methods ( design_doc , name , opts )
434
434
end
435
435
436
- # Simplified view definition. A new view will be added to the
436
+ # Simplified view definition. A new view will be added to the
437
437
# provided design document using the name and options.
438
438
#
439
- # If the view name starts with "by_" and +:by+ is not provided in
439
+ # If the view name starts with "by_" and +:by+ is not provided in
440
440
# the options, the new view's map method will be interpreted and
441
441
# generated automatically. For example:
442
442
#
443
443
# View.define(Meeting, design, "by_date_and_name")
444
444
#
445
- # Will create a view that searches by the date and name properties.
446
- # Explicity setting the attributes to use is possible using the
445
+ # Will create a view that searches by the date and name properties.
446
+ # Explicity setting the attributes to use is possible using the
447
447
# +:by+ option. For example:
448
448
#
449
449
# View.define(Meeting, design, "by_date_and_name", :by => [:date, :firstname, :lastname])
0 commit comments