Permalink
Browse files

Add query methods that don't require a callback block.

  • Loading branch information...
1 parent 4ec9a29 commit 4b203afd3ebb3eeabc066252dd97f137f2cdc306 @dgraham dgraham committed May 2, 2012
@@ -17,8 +17,10 @@
- (id)initWithBaseUrl:(NSURL *)url values:(NSDictionary *)object vines:(VinesCloud *)vines;
+- (VCDeferred *)classes;
- (VCDeferred *)classes:(VCListResultBlock)callback;
+- (VCDeferred *)channels;
- (VCDeferred *)channels:(VCListResultBlock)callback;
- (VCStorage *)storageForClass:(NSString *)className;
@@ -21,13 +21,18 @@ - (id)initWithBaseUrl:(NSURL *)url values:(NSDictionary *)object vines:(VinesClo
return self;
}
+- (VCDeferred *)classes
+{
+ return [self classes:nil];
+}
+
- (VCDeferred *)classes:(VCListResultBlock)callback
{
VCDeferred *deferred = [[VCDeferred alloc] init];
VCRequest *request = [VCRequest getWithUrl:[self url:@"/classes"]];
[request execute:^(NSMutableDictionary *result, NSHTTPURLResponse *response, VCError *error) {
if (error) {
- callback(nil, error);
+ if (callback) callback(nil, error);
[deferred reject:error];
} else {
NSArray *rows = [result valueForKey:@"rows"];
@@ -36,20 +41,25 @@ - (VCDeferred *)classes:(VCListResultBlock)callback
id storage = [[VCStorage alloc] initWithBaseUrl:[self url:@""] className:[row objectForKey:@"name"]];
[built addObject: storage];
}
- callback(built, nil);
+ if (callback) callback(built, nil);
[deferred resolve:built];
}
}];
return deferred;
}
+- (VCDeferred *)channels
+{
+ return [self channels:nil];
+}
+
- (VCDeferred *)channels:(VCListResultBlock)callback
{
VCDeferred *deferred = [[VCDeferred alloc] init];
VCRequest *request = [VCRequest getWithUrl:[self url:@"/channels"]];
[request execute:^(NSMutableDictionary *result, NSHTTPURLResponse *response, VCError *error) {
if (error) {
- callback(nil, error);
+ if (callback) callback(nil, error);
[deferred reject:error];
} else {
NSArray *rows = [result valueForKey:@"rows"];
@@ -58,7 +68,7 @@ - (VCDeferred *)channels:(VCListResultBlock)callback
id channel = [[VCChannel alloc] initWithName:[row objectForKey:@"name"] app:self];
[built addObject: channel];
}
- callback(built, nil);
+ if (callback) callback(built, nil);
[deferred resolve:built];
}
}];
@@ -23,12 +23,16 @@
- (void)where:(NSString *)queryString criteria:(NSDictionary *)criteria;
+- (VCDeferred *)find:(NSString *)objectId;
- (VCDeferred *)find:(NSString *)objectId callback:(VCObjectResultBlock)callback;
+- (VCDeferred *)all;
- (VCDeferred *)all:(VCListResultBlock)callback;
+- (VCDeferred *)first;
- (VCDeferred *)first:(VCObjectResultBlock)callback;
+- (VCDeferred *)count;
- (VCDeferred *)count:(VCCountResultBlock)callback;
- (NSString *)urlEncodeString:(NSString *)string;
@@ -36,24 +36,34 @@ - (void)where:(NSString *)queryString criteria:(NSDictionary *)aCriteria
[criteria addEntriesFromDictionary:aCriteria];
}
+- (VCDeferred *)find:(NSString *)objectId
+{
+ return [self find:objectId callback:nil];
+}
+
- (VCDeferred *)find:(NSString *)objectId callback:(VCObjectResultBlock)callback
{
VCDeferred *deferred = [[VCDeferred alloc] init];
NSString *url = [NSString stringWithFormat:@"/%@", objectId];
VCRequest *request = [VCRequest getWithUrl:[resource url:url]];
[request execute:^(NSMutableDictionary *result, NSHTTPURLResponse *response, VCError *error) {
if (error) {
- callback(nil, error);
+ if (callback) callback(nil, error);
[deferred reject:error];
} else {
id built = [resource build:result];
- callback(built, nil);
+ if (callback) callback(built, nil);
[deferred resolve:built];
}
}];
return deferred;
}
+- (VCDeferred *)all
+{
+ return [self all:nil];
+}
+
- (VCDeferred *)all:(VCListResultBlock)callback
{
NSString *json = [self jsonEncodeObject:criteria];
@@ -65,36 +75,41 @@ - (VCDeferred *)all:(VCListResultBlock)callback
VCRequest *request = [VCRequest getWithUrl:[resource url:url]];
[request execute:^(NSMutableDictionary *result, NSHTTPURLResponse *response, VCError *error) {
if (error) {
- callback(nil, error);
+ if (callback) callback(nil, error);
[deferred reject:error];
} else {
NSArray *rows = [result valueForKey:@"rows"];
NSMutableArray *built = [[NSMutableArray alloc] init];
for (NSMutableDictionary *row in rows) {
[built addObject: [resource build:row]];
}
- callback(built, nil);
+ if (callback) callback(built, nil);
[deferred resolve:built];
}
}];
return deferred;
}
+- (VCDeferred *)first
+{
+ return [self first:nil];
+}
+
- (VCDeferred *)first:(VCObjectResultBlock)callback
{
VCDeferred *deferred = [[VCDeferred alloc] init];
VCListResultBlock block = ^(NSMutableArray *rows, VCError *error) {
if (error) {
- callback(nil, error);
+ if (callback) callback(nil, error);
[deferred reject:error];
return;
}
if ([rows count] > 0) {
id first = [rows objectAtIndex:0];
- callback(first, nil);
+ if (callback) callback(first, nil);
[deferred resolve:first];
} else {
- callback(nil, nil);
+ if (callback) callback(nil, nil);
[deferred resolve:nil];
}
};
@@ -105,6 +120,11 @@ - (VCDeferred *)first:(VCObjectResultBlock)callback
return deferred;
}
+- (VCDeferred *)count
+{
+ return [self count:nil];
+}
+
- (VCDeferred *)count:(VCCountResultBlock)callback
{
NSString *json = [self jsonEncodeObject:criteria];
@@ -116,11 +136,11 @@ - (VCDeferred *)count:(VCCountResultBlock)callback
VCRequest *request = [VCRequest getWithUrl:[resource url:url]];
[request execute:^(NSMutableDictionary *result, NSHTTPURLResponse *response, VCError *error) {
if (error) {
- callback(nil, error);
+ if (callback) callback(nil, error);
[deferred reject:error];
} else {
id total = [result valueForKey:@"total"];
- callback(total, nil);
+ if (callback) callback(total, nil);
[deferred resolve:total];
}
}];
@@ -17,10 +17,13 @@
- (VCQuery *)queryWithCriteria:(NSDictionary *)criteria;
+- (VCDeferred *)save:(NSMutableDictionary *)object;
- (VCDeferred *)save:(NSMutableDictionary *)object callback:(VCObjectResultBlock)callback;
+- (VCDeferred *)remove:(NSMutableDictionary *)options;
- (VCDeferred *)remove:(NSMutableDictionary *)options callback:(VCObjectResultBlock)callback;
+- (VCDeferred *)removeById:(NSString *)objectId;
- (VCDeferred *)removeById:(NSString *)objectId callback:(VCObjectResultBlock)callback;
- (id)build:(NSMutableDictionary *)object;
@@ -39,6 +39,11 @@ - (VCQuery *)queryWithCriteria:(NSDictionary *)criteria
return query;
}
+- (VCDeferred *)save:(NSMutableDictionary *)object
+{
+ return [self save:object callback:nil];
+}
+
- (VCDeferred *)save:(NSMutableDictionary *)object callback:(VCObjectResultBlock)callback
{
NSError *error;
@@ -48,7 +53,7 @@ - (VCDeferred *)save:(NSMutableDictionary *)object callback:(VCObjectResultBlock
VCDeferred *deferred = [[VCDeferred alloc] init];
void (^block)(NSMutableDictionary *, NSHTTPURLResponse *, VCError *) = ^(NSMutableDictionary *result, NSHTTPURLResponse *response, VCError *error) {
if (error) {
- callback(nil, error);
+ if (callback) callback(nil, error);
[deferred reject:error];
return;
}
@@ -58,7 +63,7 @@ - (VCDeferred *)save:(NSMutableDictionary *)object callback:(VCObjectResultBlock
NSString *objectId = [pieces objectAtIndex:[pieces count] - 1];
[object setObject:objectId forKey:@"id"];
}
- callback(object, nil);
+ if (callback) callback(object, nil);
[deferred resolve:object];
};
@@ -73,23 +78,33 @@ - (VCDeferred *)save:(NSMutableDictionary *)object callback:(VCObjectResultBlock
return deferred;
}
+- (VCDeferred *)removeById:(NSString *)objectId
+{
+ return [self removeById:objectId callback:nil];
+}
+
- (VCDeferred *)removeById:(NSString *)objectId callback:(VCObjectResultBlock)callback
{
NSMutableDictionary *criteria = [[NSMutableDictionary alloc] initWithObjectsAndKeys:objectId, @"id", nil];
return [self remove:criteria callback:callback];
}
+- (VCDeferred *)remove:(NSMutableDictionary *)object
+{
+ return [self remove:object callback:nil];
+}
+
- (VCDeferred *)remove:(NSMutableDictionary *)object callback:(VCObjectResultBlock)callback
{
VCDeferred *deferred = [[VCDeferred alloc] init];
NSString *url = [NSString stringWithFormat:@"/%@", [object valueForKey:@"id"]];
VCRequest *request = [VCRequest deleteWithUrl:[self url:url]];
[request execute:^(NSMutableDictionary *result, NSHTTPURLResponse *response, VCError *error) {
if (error) {
- callback(nil, error);
+ if (callback) callback(nil, error);
[deferred reject:error];
} else {
- callback(object, nil);
+ if (callback) callback(object, nil);
[deferred resolve:object];
}
}];

0 comments on commit 4b203af

Please sign in to comment.