Skip to content

Commit

Permalink
New init method on Database. Better JSON method names.
Browse files Browse the repository at this point in the history
  • Loading branch information
schwa committed Nov 10, 2010
1 parent 39c330d commit 4fe5659
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 37 deletions.
55 changes: 22 additions & 33 deletions Source/CCouchDBDatabase.m
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions Source/CCouchDBDocument.h
Expand Up @@ -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;

Expand Down
14 changes: 12 additions & 2 deletions Source/CCouchDBDocument.m
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
{
Expand All @@ -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]];
Expand Down

0 comments on commit 4fe5659

Please sign in to comment.