Permalink
Browse files

Support va_list in where:, find:, and countWhere:

This removes support for whereFormat: and findWithFormat: in the
process, so upgrade your calls!

Signed-off-by: Stephen Celis <stephen@stephencelis.com>
  • Loading branch information...
stephencelis committed Mar 6, 2014
1 parent 26649cf commit 2e17e1466872b75f1ea38f720ab3ce0f5bd95a62
@@ -29,16 +29,14 @@
+ (NSArray *)all;
+ (NSArray *)allWithOrder:(id)order;
-+ (NSArray *)where:(id)condition;
++ (NSArray *)where:(id)condition, ...;
+ (NSArray *)where:(id)condition order:(id)order;
+ (NSArray *)where:(id)condition limit:(NSNumber *)limit;
+ (NSArray *)where:(id)condition order:(id)order limit:(NSNumber *)limit;
-+ (NSArray *)whereFormat:(NSString *)format, ...;
+ (instancetype)findOrCreate:(NSDictionary *)attributes;
-+ (instancetype)find:(id)condition;
-+ (instancetype)findWithFormat:(NSString *)format, ...;
++ (instancetype)find:(id)condition, ...;
+ (NSUInteger)count;
-+ (NSUInteger)countWhere:(id)condition;
++ (NSUInteger)countWhere:(id)condition, ...;
#pragma mark - Custom Context
@@ -45,15 +45,6 @@ + (NSArray *)allInContext:(NSManagedObjectContext *)context order:(id)order {
return [self fetchWithCondition:nil inContext:context withOrder:order fetchLimit:nil];
}
-+ (NSArray *)whereFormat:(NSString *)format, ... {
- va_list va_arguments;
- va_start(va_arguments, format);
- NSPredicate *condition = [NSPredicate predicateWithFormat:format arguments:va_arguments];
- va_end(va_arguments);
-
- return [self where:condition];
-}
-
+ (instancetype)findOrCreate:(NSDictionary *)properties {
return [self findOrCreate:properties inContext:[NSManagedObjectContext defaultContext]];
}
@@ -63,25 +54,26 @@ + (instancetype)findOrCreate:(NSDictionary *)properties inContext:(NSManagedObje
return existing ?: [self create:properties inContext:context];
}
-+ (instancetype)findWithFormat:(NSString *)format, ... {
++ (instancetype)find:(id)condition, ... {
va_list va_arguments;
- va_start(va_arguments, format);
- NSPredicate *condition = [NSPredicate predicateWithFormat:format arguments:va_arguments];
+ va_start(va_arguments, condition);
+ NSPredicate *predicate = [self predicateFromObject:condition arguments:va_arguments];
va_end(va_arguments);
- return [self find:condition];
-}
-
-+ (instancetype)find:(id)condition {
- return [self find:condition inContext:[NSManagedObjectContext defaultContext]];
+ return [self find:predicate inContext:[NSManagedObjectContext defaultContext]];
}
+ (instancetype)find:(id)condition inContext:(NSManagedObjectContext *)context {
return [self where:condition inContext:context limit:@1].first;
}
-+ (NSArray *)where:(id)condition {
- return [self where:condition inContext:[NSManagedObjectContext defaultContext]];
++ (NSArray *)where:(id)condition, ... {
+ va_list va_arguments;
+ va_start(va_arguments, condition);
+ NSPredicate *predicate = [self predicateFromObject:condition arguments:va_arguments];
+ va_end(va_arguments);
+
+ return [self where:predicate inContext:[NSManagedObjectContext defaultContext]];
}
+ (NSArray *)where:(id)condition order:(id)order {
@@ -118,8 +110,13 @@ + (NSUInteger)count {
return [self countInContext:[NSManagedObjectContext defaultContext]];
}
-+ (NSUInteger)countWhere:(id)condition {
- return [self countWhere:condition inContext:[NSManagedObjectContext defaultContext]];
++ (NSUInteger)countWhere:(id)condition, ... {
+ va_list va_arguments;
+ va_start(va_arguments, condition);
+ NSPredicate *predicate = [self predicateFromObject:condition arguments:va_arguments];
+ va_end(va_arguments);
+
+ return [self countWhere:predicate inContext:[NSManagedObjectContext defaultContext]];
}
+ (NSUInteger)countInContext:(NSManagedObjectContext *)context {
@@ -204,13 +201,18 @@ + (NSPredicate *)predicateFromDictionary:(NSDictionary *)dict {
return [NSCompoundPredicate andPredicateWithSubpredicates:subpredicates];
}
-+ (NSPredicate *)predicateFromObject:(id)condition {
++ (NSPredicate *)predicateFromObject:(id)condition
+{
+ return [self predicateFromObject:condition arguments:NULL];
+}
++ (NSPredicate *)predicateFromObject:(id)condition arguments:(va_list)arguments
+{
if ([condition isKindOfClass:[NSPredicate class]])
return condition;
- else if ([condition isKindOfClass:[NSString class]])
- return [NSPredicate predicateWithFormat:condition];
+ if ([condition isKindOfClass:[NSString class]])
+ return [NSPredicate predicateWithFormat:condition arguments:arguments];
else if ([condition isKindOfClass:[NSDictionary class]])
return [self predicateFromDictionary:condition];
@@ -70,7 +70,7 @@ void createSomePeople(NSArray *names, NSArray *surnames, NSManagedObjectContext
it(@"Finds using [Entity where: STRING and ARGUMENTS]", ^{
- Person *unique = [Person whereFormat:@"firstName == %@", UNIQUE_NAME].first;
+ Person *unique = [Person where:@"firstName == %@", UNIQUE_NAME].first;
[[unique.lastName should] equal:UNIQUE_SURNAME];
});
@@ -117,7 +117,7 @@ void createSomePeople(NSArray *names, NSArray *surnames, NSManagedObjectContext
});
it(@"Finds the first match using [Entity find: STRING and ARGUMENTS]", ^{
- Person *johnDoe = [Person findWithFormat:@"firstName = %@ AND lastName = %@", @"John", @"Doe"];
+ Person *johnDoe = [Person find:@"firstName = %@ AND lastName = %@", @"John", @"Doe"];
[[johnDoe.firstName should] equal:@"John"];
});
@@ -217,6 +217,11 @@ void createSomePeople(NSArray *names, NSArray *surnames, NSManagedObjectContext
NSUInteger count = [Person countWhere:@{@"firstName" : @"Nobody"}];
[[@(count) should] equal:@(0)];
});
+
+ it(@"counts with variable arguments", ^{
+ NSUInteger count = [Person countWhere:@"firstName = %@", @"Neo"];
+ [[@(count) should] equal:@(1)];
+ });
});
context(@"Creating", ^{

0 comments on commit 2e17e14

Please sign in to comment.