Skip to content
Browse files

Filter-function API in CouchDesignDocument

Not tested yet!
  • Loading branch information...
1 parent 2bfc8cd commit 70f139c75685ca2bd6c725cb1c22434c3d692eb0 @snej snej committed Feb 22, 2012
Showing with 46 additions and 5 deletions.
  1. +13 −0 Couch/CouchDesignDocument.h
  2. +33 −5 Couch/CouchDesignDocument.m
View
13 Couch/CouchDesignDocument.h
@@ -30,6 +30,7 @@ extern NSString* const kCouchLanguageErlang;
@private
NSString* _language;
NSMutableDictionary* _views;
+ NSMutableDictionary* _filters;
NSString* _validation;
NSMutableDictionary* _viewOptions;
NSString* _viewsRevisionID;
@@ -78,6 +79,18 @@ extern NSString* const kCouchLanguageErlang;
- (void) defineViewNamed: (NSString*)viewName
map: (NSString*)mapFunction;
+/** The filter functions defined in this design document.
+ @return An NSDictionary whose keys are filter names and values are function source code. */
+@property (readonly) NSDictionary* filters;
+
+/** Sets the definition of a filter, or deletes it.
+ After making changes to one or more filters, you should call -saveChanges to PUT them back to the database.
+ If the new definition is identical to the existing one, the design document will not be marked as changed or saved back to the database.
+ @param filterName The name of the filter, in the scope of this design doc.
+ @param filterFunction The source code of the filter function. If nil, the filter will be deleted. */
+- (void) defineFilterNamed: (NSString*)filterName
+ asFunction: (NSString*)filterFunction;
+
/** The validation function, a JavaScript function that validates document contents. */
@property (copy) NSString* validation;
View
38 Couch/CouchDesignDocument.m
@@ -34,6 +34,7 @@ - (void)dealloc {
[_validation release];
[_language release];
[_views release];
+ [_filters release];
[_viewsRevisionID release];
[_viewOptions release];
[super dealloc];
@@ -80,22 +81,30 @@ - (void) setLanguage:(NSString *)language {
}
+static NSMutableDictionary* mutableCopyProperty(NSDictionary* properties,
+ NSString* key)
+{
+ NSDictionary* dict = $castIf(NSDictionary, [properties objectForKey: key]);
+ return dict ? [dict mutableCopy] : [[NSMutableDictionary alloc] init];
+}
+
+
/** Returns a dictionary mapping view names to the dictionaries defining them (as in the design document's JSON source.)
The first call fetches the entire design document; subsequent calls are cached. */
- (NSDictionary*) views {
if (_views && !$equal(_viewsRevisionID, self.currentRevisionID)) {
// cache is invalid now:
[_views release];
_views = nil;
+ [_filters release];
+ _filters = nil;
[_viewsRevisionID release];
_viewsRevisionID = nil;
}
if (!_views) {
- NSDictionary* views = $castIf(NSDictionary, [self.properties objectForKey: @"views"]);
- if (views)
- _views = [views mutableCopy];
- else
- _views = [[NSMutableDictionary alloc] init];
+ NSDictionary* properties = self.properties;
+ _views = mutableCopyProperty(properties, @"views");
+ _filters = mutableCopyProperty(properties, @"filters");
_viewsRevisionID = [self.currentRevisionID copy];
}
return _views;
@@ -152,6 +161,23 @@ - (void) defineViewNamed: (NSString*)viewName
}
+- (NSDictionary*) filters {
+ [self views]; // This also loads/instantiates _filters
+ return _filters;
+}
+
+
+- (void) defineFilterNamed: (NSString*)filterName
+ asFunction: (NSString*)filterFunction
+{
+ [self views]; // This also loads/instantiates _filters
+ if (!$equal(filterFunction, [_filters objectForKey: filterName])) {
+ [_filters setValue: filterFunction forKey: filterName];
+ self.changed = YES;
+ }
+}
+
+
- (NSString*) validation {
if (_changedValidation)
return _validation;
@@ -206,6 +232,8 @@ - (RESTOperation*) saveChanges {
newProps = [NSMutableDictionary dictionary];
if (_views)
[newProps setObject: _views forKey: @"views"];
+ if (_filters)
+ [newProps setObject: _filters forKey: @"filters"];
if (_language)
[newProps setValue: _language forKey: @"language"];
if (_changedValidation)

0 comments on commit 70f139c

Please sign in to comment.
Something went wrong with that request. Please try again.