Skip to content

Commit

Permalink
Merge pull request supermarin#80 from stephencelis/find-with-condition
Browse files Browse the repository at this point in the history
Find with condition
  • Loading branch information
Marin Usalj committed Mar 6, 2014
2 parents 1352e8d + b872565 commit 26649cf
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
5 changes: 3 additions & 2 deletions Classes/NSManagedObject+ActiveRecord.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
+ (NSArray *)where:(id)condition order:(id)order limit:(NSNumber *)limit;
+ (NSArray *)whereFormat:(NSString *)format, ...;
+ (instancetype)findOrCreate:(NSDictionary *)attributes;
+ (instancetype)find:(NSDictionary *)attributes;
+ (instancetype)find:(id)condition;
+ (instancetype)findWithFormat:(NSString *)format, ...;
+ (NSUInteger)count;
+ (NSUInteger)countWhere:(id)condition;

Expand All @@ -53,7 +54,7 @@
+ (NSArray *)where:(id)condition inContext:(NSManagedObjectContext *)context limit:(NSNumber *)limit;
+ (NSArray *)where:(id)condition inContext:(NSManagedObjectContext *)context order:(id)order limit:(NSNumber *)limit;
+ (instancetype)findOrCreate:(NSDictionary *)properties inContext:(NSManagedObjectContext *)context;
+ (instancetype)find:(NSDictionary *)attributes inContext:(NSManagedObjectContext *)context;
+ (instancetype)find:(id)condition inContext:(NSManagedObjectContext *)context;
+ (NSUInteger)countInContext:(NSManagedObjectContext *)context;
+ (NSUInteger)countWhere:(id)condition inContext:(NSManagedObjectContext *)context;

Expand Down
19 changes: 14 additions & 5 deletions Classes/NSManagedObject+ActiveRecord.m
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ + (NSArray *)allInContext:(NSManagedObjectContext *)context order:(id)order {
+ (NSArray *)whereFormat:(NSString *)format, ... {
va_list va_arguments;
va_start(va_arguments, format);
NSString *condition = [[NSString alloc] initWithFormat:format arguments:va_arguments];
NSPredicate *condition = [NSPredicate predicateWithFormat:format arguments:va_arguments];
va_end(va_arguments);

return [self where:condition];
Expand All @@ -63,12 +63,21 @@ + (instancetype)findOrCreate:(NSDictionary *)properties inContext:(NSManagedObje
return existing ?: [self create:properties inContext:context];
}

+ (instancetype)find:(NSDictionary *)attributes {
return [self find:attributes inContext:[NSManagedObjectContext defaultContext]];
+ (instancetype)findWithFormat:(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 find:condition];
}

+ (instancetype)find:(id)condition {
return [self find:condition inContext:[NSManagedObjectContext defaultContext]];
}

+ (instancetype)find:(NSDictionary *)attributes inContext:(NSManagedObjectContext *)context {
return [self where:attributes inContext:context limit:@1].first;
+ (instancetype)find:(id)condition inContext:(NSManagedObjectContext *)context {
return [self where:condition inContext:context limit:@1].first;
}

+ (NSArray *)where:(id)condition {
Expand Down
14 changes: 12 additions & 2 deletions Example/SampleProjectTests/FindersAndCreatorsTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ void createSomePeople(NSArray *names, NSArray *surnames, NSManagedObjectContext

it(@"Finds using [Entity where: STRING]", ^{

Person *unique = [Person where:[NSString stringWithFormat:@"firstName == '%@'",UNIQUE_NAME]].first;
Person *unique = [Person where:[NSPredicate predicateWithFormat:@"firstName == %@",UNIQUE_NAME]].first;
[[unique.lastName should] equal:UNIQUE_SURNAME];

});

it(@"Finds using [Entity where: STRING and ARGUMENTS]", ^{

Person *unique = [Person whereFormat:@"firstName == '%@'", UNIQUE_NAME].first;
Person *unique = [Person whereFormat:@"firstName == %@", UNIQUE_NAME].first;
[[unique.lastName should] equal:UNIQUE_SURNAME];

});
Expand Down Expand Up @@ -111,6 +111,16 @@ void createSomePeople(NSArray *names, NSArray *surnames, NSManagedObjectContext
[[johnDoe.firstName should] equal:@"John"];
});

it(@"Finds the first match using [Entity find: STRING]", ^{
Person *johnDoe = [Person find:@"firstName = 'John' AND lastName = 'Doe'"];
[[johnDoe.firstName should] equal:@"John"];
});

it(@"Finds the first match using [Entity find: STRING and ARGUMENTS]", ^{
Person *johnDoe = [Person findWithFormat:@"firstName = %@ AND lastName = %@", @"John", @"Doe"];
[[johnDoe.firstName should] equal:@"John"];
});

it(@"doesn't create an object on find", ^{
Person *cat = [Person find:@{ @"firstName": @"Cat" }];
[cat shouldBeNil];
Expand Down

0 comments on commit 26649cf

Please sign in to comment.