Eliminate connection matchers in favor of source and destination predicates. closes #1105

This commit is contained in:
Blake Watters
2012-12-24 18:29:00 -05:00
parent ea304c5ec4
commit 285b75ea2a
4 changed files with 11 additions and 16 deletions

View File

@@ -19,7 +19,6 @@
//
#import <CoreData/CoreData.h>
#import "RKDynamicMappingMatcher.h"
/**
The `RKConnectionDescription` class describes a means for connecting a Core Data relationship. Connections can be established either by foreign key, in which case one or more attribute values on the source entity correspond to matching values on the destination entity, or by key path, in which case a key path is evaluated on the object graph to obtain a value for the relationship. Connection objects are used by instances of `RKRelationshipConnectionOperation` to connect a relationship of a given managed object.
@@ -153,13 +152,13 @@
@property (nonatomic, assign) BOOL includesSubentities;
/**
An optional predicate for filtering objects to be connected.
An optional predicate for conditionally evaluating the connection based on the state of the source object.
*/
@property (nonatomic, copy) NSPredicate *predicate;
@property (nonatomic, strong) NSPredicate *sourcePredicate;
/**
An optional matcher that filters the source object to be connected.
An optional predicate for filtering objects to be connected.
*/
@property (nonatomic, strong) RKDynamicMappingMatcher* matcher;
@property (nonatomic, copy) NSPredicate *destinationPredicate;
@end

View File

@@ -80,7 +80,7 @@ static NSSet *RKSetWithInvalidAttributesForEntity(NSArray *attributes, NSEntityD
if ([self class] == [RKConnectionDescription class]) {
@throw [NSException exceptionWithName:NSInternalInconsistencyException
reason:[NSString stringWithFormat:@"%@ Failed to call designated initializer. "
"Invoke initWithRelationship:sourceKeyPath:destinationKeyPath:matcher: instead.",
"Invoke initWithRelationship:attributes: instead.",
NSStringFromClass([self class])]
userInfo:nil];
}

View File

@@ -139,16 +139,14 @@ static NSDictionary *RKConnectionAttributeValuesWithObject(RKConnectionDescripti
- (id)findConnected
{
id connectionResult = nil;
if (self.connection.matcher && ![self.connection.matcher matches:self.managedObject])
return nil;
if (self.connection.sourcePredicate && ![self.connection.sourcePredicate evaluateWithObject:self.managedObject]) return nil;
if ([self.connection isForeignKeyConnection]) {
NSDictionary *attributeValues = RKConnectionAttributeValuesWithObject(self.connection, self.managedObject);
NSSet *managedObjects = [self.managedObjectCache managedObjectsWithEntity:[self.connection.relationship destinationEntity]
attributeValues:attributeValues
inManagedObjectContext:self.managedObjectContext];
if (self.connection.predicate) managedObjects = [managedObjects filteredSetUsingPredicate:self.connection.predicate];
if (self.connection.destinationPredicate) managedObjects = [managedObjects filteredSetUsingPredicate:self.connection.destinationPredicate];
if (!self.connection.includesSubentities) managedObjects = [managedObjects filteredSetUsingPredicate:[NSPredicate predicateWithFormat:@"entity == %@", [self.connection.relationship destinationEntity]]];
if ([self.connection.relationship isToMany]) {
connectionResult = managedObjects;

View File

@@ -256,7 +256,7 @@
expect(secondChild.friends).to.equal(expectedFriends);
}
- (void)testConnectionMatcher
- (void)testConnectionWithSourcePredicate
{
RKHuman *human = [RKTestFactory insertManagedObjectForEntityForName:@"Human" inManagedObjectContext:nil withProperties:nil];
human.sex = @"female";
@@ -271,16 +271,14 @@
[mapping addConnectionForRelationship:@"cats" connectedBy:@"sex"];
RKFetchRequestManagedObjectCache *managedObjectCache = [RKFetchRequestManagedObjectCache new];
RKConnectionDescription *connection = [mapping connectionForRelationship:@"cats"];
RKEntityMapping *catMapping = [RKEntityMapping mappingForEntityForName:@"Cat" inManagedObjectStore:[RKTestFactory managedObjectStore]];
connection.matcher = [[RKDynamicMappingMatcher alloc] initWithKeyPath:@"sex" expectedValue:@"male" objectMapping:catMapping];
connection.sourcePredicate = [NSPredicate predicateWithFormat:@"sex == %@", @"male"];
RKRelationshipConnectionOperation *operation = [[RKRelationshipConnectionOperation alloc] initWithManagedObject:human connection:connection managedObjectCache:managedObjectCache];
[operation start];
assertThat(human.cats, hasCountOf(0));
}
- (void)testConnectionPredicate
- (void)testConnectionWithDestinationPredicate
{
RKHuman *human = [RKTestFactory insertManagedObjectForEntityForName:@"Human" inManagedObjectContext:nil withProperties:nil];
human.sex = @"female";
@@ -294,7 +292,7 @@
[mapping addConnectionForRelationship:@"cats" connectedBy:@"sex"];
RKFetchRequestManagedObjectCache *managedObjectCache = [RKFetchRequestManagedObjectCache new];
RKConnectionDescription *connection = [mapping connectionForRelationship:@"cats"];
connection.predicate = [NSPredicate predicateWithFormat:@"birthYear = 2011"];
connection.destinationPredicate = [NSPredicate predicateWithFormat:@"birthYear = 2011"];
RKRelationshipConnectionOperation *operation = [[RKRelationshipConnectionOperation alloc] initWithManagedObject:human connection:connection managedObjectCache:managedObjectCache];