From 4fe5659a07a9b2a709d3cae533d61f9725920eb2 Mon Sep 17 00:00:00 2001 From: Jonathan Wight Date: Wed, 10 Nov 2010 11:08:47 -0800 Subject: [PATCH] New init method on Database. Better JSON method names. --- Source/CCouchDBDatabase.m | 55 ++++++++++++++++----------------------- Source/CCouchDBDocument.h | 5 ++-- Source/CCouchDBDocument.m | 14 ++++++++-- 3 files changed, 37 insertions(+), 37 deletions(-) diff --git a/Source/CCouchDBDatabase.m b/Source/CCouchDBDatabase.m index 72f885a..893539b 100644 --- a/Source/CCouchDBDatabase.m +++ b/Source/CCouchDBDatabase.m @@ -132,7 +132,7 @@ - (CURLOperation *)operationToCreateDocument:(NSDictionary *)inDocument successH NSString *theRevision = [inParameter objectForKey:@"rev"]; CCouchDBDocument *theDocument = [[[CCouchDBDocument alloc] initWithDatabase:self identifier:theIdentifier revision:theRevision] autorelease]; - [theDocument populateWithJSONDictionary:inDocument]; + [theDocument populateWithJSON:inDocument]; if (inSuccessHandler) inSuccessHandler(theDocument); @@ -166,7 +166,7 @@ - (CURLOperation *)operationToCreateDocument:(NSDictionary *)inDocument identifi NSString *theRevision = [inParameter objectForKey:@"rev"]; CCouchDBDocument *theDocument = [[[CCouchDBDocument alloc] initWithDatabase:self identifier:inIdentifier revision:theRevision] autorelease]; - [theDocument populateWithJSONDictionary:inDocument]; + [theDocument populateWithJSON:inDocument]; if (inSuccessHandler) inSuccessHandler(theDocument); @@ -179,29 +179,7 @@ - (CURLOperation *)operationToCreateDocument:(NSDictionary *)inDocument identifi - (CURLOperation *)operationToFetchAllDocumentsWithOptions:(NSDictionary *)inOptions withSuccessHandler:(CouchDBSuccessHandler)inSuccessHandler failureHandler:(CouchDBFailureHandler)inFailureHandler { - NSURL *theURL = [self.URL URLByAppendingPathComponent:@"_all_docs"]; - NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:theURL]; - theRequest.HTTPMethod = @"GET"; - [theRequest setValue:kContentTypeJSON forHTTPHeaderField:@"Accept"]; - CCouchDBURLOperation *theOperation = [self.session URLOperationWithRequest:theRequest]; - theOperation.successHandler = ^(id inParameter) { - NSMutableArray *theDocuments = [NSMutableArray array]; - for (NSDictionary *theRow in [inParameter objectForKey:@"rows"]) - { - NSString *theIdentifier = [theRow objectForKey:@"id"]; - - CCouchDBDocument *theDocument = [[[CCouchDBDocument alloc] initWithDatabase:self identifier:theIdentifier] autorelease]; - theDocument.revision = [theRow valueForKeyPath:@"value.rev"]; - - [theDocuments addObject:theDocument]; - } - - if (inSuccessHandler) - inSuccessHandler(theDocuments); - }; - theOperation.failureHandler = inFailureHandler; - - return(theOperation); + return([self operationToBulkFetchDocuments:NULL options:NULL successHandler:inSuccessHandler failureHandler:inFailureHandler]); } - (CURLOperation *)operationToFetchDocumentForIdentifier:(NSString *)inIdentifier options:(NSDictionary *)inOptions successHandler:(CouchDBSuccessHandler)inSuccessHandler failureHandler:(CouchDBFailureHandler)inFailureHandler @@ -214,7 +192,7 @@ - (CURLOperation *)operationToFetchDocumentForIdentifier:(NSString *)inIdentifie theOperation.successHandler = ^(id inParameter) { CCouchDBDocument *theDocument = [[[CCouchDBDocument alloc] initWithDatabase:self] autorelease]; - [theDocument populateWithJSONDictionary:inParameter]; + [theDocument populateWithJSON:inParameter]; if (inSuccessHandler) inSuccessHandler(theDocument); @@ -233,7 +211,7 @@ - (CURLOperation *)operationToFetchDocument:(CCouchDBDocument *)inDocument optio [theRequest setValue:kContentTypeJSON forHTTPHeaderField:@"Accept"]; CCouchDBURLOperation *theOperation = [self.session URLOperationWithRequest:theRequest]; theOperation.successHandler = ^(id inParameter) { - [inDocument populateWithJSONDictionary:inParameter]; + [inDocument populateWithJSON:inParameter]; if (inSuccessHandler) inSuccessHandler(inDocument); @@ -257,7 +235,7 @@ - (CURLOperation *)operationToUpdateDocument:(CCouchDBDocument *)inDocument succ CCouchDBURLOperation *theOperation = [self.session URLOperationWithRequest:theRequest]; theOperation.successHandler = ^(id inParameter) { - [inDocument populateWithJSONDictionary:inParameter]; + [inDocument populateWithJSON:inParameter]; if (inSuccessHandler) inSuccessHandler(inDocument); @@ -296,7 +274,7 @@ - (CURLOperation *)operationToFetchChanges:(NSDictionary *)inOptions successHand CCouchDBURLOperation *theOperation = [[[CCouchDBURLOperation alloc] initWithSession:self.session request:theRequest] autorelease]; theOperation.successHandler = ^(id inParameter) { - CCouchDBChangeSet *theChangeSet = [[[CCouchDBChangeSet alloc] initWithJSON:inParameter] autorelease]; + CCouchDBChangeSet *theChangeSet = [[[CCouchDBChangeSet alloc] initWithDatabase:self JSON:inParameter] autorelease]; if (inSuccessHandler) inSuccessHandler(theChangeSet); @@ -390,12 +368,23 @@ - (CURLOperation *)operationToBulkFetchDocuments:(NSArray *)inDocuments options: NSMutableArray *theDocuments = [NSMutableArray array]; for (NSDictionary *theRow in [inParameter objectForKey:@"rows"]) { - NSString *theIdentifier = [theRow objectForKey:@"id"]; + NSDictionary *doc = [theRow objectForKey:@"doc"]; + if (doc) + { + CCouchDBDocument *theDocument = [[[CCouchDBDocument alloc] initWithDatabase:self] autorelease]; + [theDocument populateWithJSON:doc]; + + [theDocuments addObject:theDocument]; + } + else + { + NSString *theIdentifier = [theRow objectForKey:@"id"]; - CCouchDBDocument *theDocument = [[[CCouchDBDocument alloc] initWithDatabase:self identifier:theIdentifier] autorelease]; - theDocument.revision = [theRow valueForKeyPath:@"value.rev"]; + CCouchDBDocument *theDocument = [[[CCouchDBDocument alloc] initWithDatabase:self identifier:theIdentifier] autorelease]; + theDocument.revision = [theRow valueForKeyPath:@"value.rev"]; - [theDocuments addObject:theDocument]; + [theDocuments addObject:theDocument]; + } } if (inSuccessHandler) diff --git a/Source/CCouchDBDocument.h b/Source/CCouchDBDocument.h index db67c35..effa522 100644 --- a/Source/CCouchDBDocument.h +++ b/Source/CCouchDBDocument.h @@ -28,9 +28,10 @@ - (id)initWithDatabase:(CCouchDBDatabase *)inDatabase; - (id)initWithDatabase:(CCouchDBDatabase *)inDatabase identifier:(NSString *)inIdentifier; - (id)initWithDatabase:(CCouchDBDatabase *)inDatabase identifier:(NSString *)inIdentifier revision:(NSString *)inRevision; +- (id)initWithDatabase:(CCouchDBDatabase *)inDatabase JSON:(id)inJSON; -- (void)populateWithJSONDictionary:(NSDictionary *)inDictionary; -- (NSDictionary *)asJSONDictionary; +- (void)populateWithJSON:(id)inDictionary; +- (id)asJSON; - (void)addAttachment:(CCouchDBAttachment *)inAttachment; diff --git a/Source/CCouchDBDocument.m b/Source/CCouchDBDocument.m index 10a8585..c0970aa 100644 --- a/Source/CCouchDBDocument.m +++ b/Source/CCouchDBDocument.m @@ -66,6 +66,15 @@ - (id)initWithDatabase:(CCouchDBDatabase *)inDatabase identifier:(NSString *)inI return(self); } +- (id)initWithDatabase:(CCouchDBDatabase *)inDatabase JSON:(id)inJSON; +{ +if ((self = [self initWithDatabase:inDatabase]) != NULL) + { + [self populateWithJSON:inJSON]; + } +return(self); +} + - (void)dealloc { database = NULL; @@ -185,12 +194,12 @@ - (CCouchDBSession *)session #pragma mark - -- (void)populateWithJSONDictionary:(NSDictionary *)inDictionary +- (void)populateWithJSON:(id)inDictionary { self.content = inDictionary; } -- (NSDictionary *)asJSONDictionary +- (id)asJSON { if (self.content) { @@ -205,6 +214,7 @@ - (NSDictionary *)asJSONDictionary } } +// TODO this is just a quick hack for attachments. Needs a lot more work. - (void)addAttachment:(CCouchDBAttachment *)inAttachment { NSURL *theURL = [[self.URL absoluteURL] URLByAppendingPathComponent:[NSString stringWithFormat:@"%@?rev=%@", inAttachment.identifier, self.revision]];