11
11
from copy import deepcopy
12
12
from itertools import groupby
13
13
from operator import attrgetter
14
+ from textwrap import dedent
14
15
15
16
16
17
class ViewDefinition (object ):
17
- """Definition of a view stored in a specific design document.
18
+ r """Definition of a view stored in a specific design document.
18
19
19
20
An instance of this class can be used to access the results of the view,
20
21
as well as to keep the view definition in the design document up to date
@@ -53,6 +54,10 @@ def __init__(self, design, name, map_fun, reduce_fun=None,
53
54
language = 'javascript' , wrapper = None ):
54
55
"""Initialize the view definition.
55
56
57
+ Note that the code in `map_fun` and `reduce_fun` is automatically
58
+ dedented, that is, any common leading whitespace is removed from each
59
+ line.
60
+
56
61
:param design: the name of the design document
57
62
:param name: the name of the view
58
63
:param map_fun: the map function code
@@ -65,7 +70,9 @@ def __init__(self, design, name, map_fun, reduce_fun=None,
65
70
design = design [8 :]
66
71
self .design = design
67
72
self .name = name
68
- self .map_fun = map_fun
73
+ self .map_fun = dedent (map_fun .lstrip ('\n \r ' ))
74
+ if reduce_fun :
75
+ reduce_fun = dedent (reduce_fun .lstrip ('\n \r ' ))
69
76
self .reduce_fun = reduce_fun
70
77
self .language = language
71
78
self .wrapper = wrapper
@@ -121,7 +128,8 @@ def sync_many(db, views, remove_missing=False, callback=None):
121
128
instances should be removed
122
129
:param callback: a callback function that is invoked when a design
123
130
document gets updated; the callback gets passed the
124
- design document as only parameter
131
+ design document as only parameter, before that doc
132
+ has actually been saved back to the database
125
133
"""
126
134
docs = []
127
135
0 commit comments