mirror of
https://github.com/zhigang1992/RestKit.git
synced 2026-04-05 09:39:09 +08:00
* Replaces primary key with `RKEntityIdentifier` * Add support for use of compound keys for object identification * Refactor `RKConnectionMapping` to `RKConnectionDescription` and add support for connecting with multiple attributes * Clarify naming of representation key methods to better match naming conventions * Add type transformation support for object identification * Greatly expand test coverage for object identification * Drop the `NSEntityDescription` category * Simplify the `RKManagedObjectCaching` protocol * Add compound key support to the Fetch Request and In Memory Cache implementations * Replace Kiwi with Specta for tests where contexts are helpful for organization * Rename `defaultValueForMissingAttribute` to `defaultValueForAttribute`
130 lines
4.5 KiB
Objective-C
130 lines
4.5 KiB
Objective-C
//
|
|
// RKConnectionDescription.m
|
|
// RestKit
|
|
//
|
|
// Created by Blake Watters on 11/20/12.
|
|
// Copyright (c) 2012 RestKit. All rights reserved.
|
|
//
|
|
|
|
#import "RKConnectionDescription.h"
|
|
|
|
static NSSet *RKSetWithInvalidAttributesForEntity(NSArray *attributes, NSEntityDescription *entity)
|
|
{
|
|
NSMutableSet *attributesSet = [NSMutableSet setWithArray:attributes];
|
|
NSSet *validAttributeNames = [NSSet setWithArray:[[entity attributesByName] allKeys]];
|
|
[attributesSet minusSet:validAttributeNames];
|
|
return attributesSet;
|
|
}
|
|
|
|
// Provides support for connecting a relationship by
|
|
@interface RKForeignKeyConnectionDescription : RKConnectionDescription
|
|
@end
|
|
|
|
// Provides support for connecting a relationship by traversing the object graph
|
|
@interface RKKeyPathConnectionDescription : RKConnectionDescription
|
|
@end
|
|
|
|
@interface RKConnectionDescription ()
|
|
@property (nonatomic, strong, readwrite) NSRelationshipDescription *relationship;
|
|
@property (nonatomic, copy, readwrite) NSDictionary *attributes;
|
|
@property (nonatomic, copy, readwrite) NSString *keyPath;
|
|
@end
|
|
|
|
@implementation RKConnectionDescription
|
|
|
|
- (id)initWithRelationship:(NSRelationshipDescription *)relationship attributes:(NSDictionary *)attributes
|
|
{
|
|
NSParameterAssert(relationship);
|
|
NSParameterAssert(attributes);
|
|
NSAssert([attributes count], @"Cannot connect a relationship without at least one pair of attributes describing the connection");
|
|
NSSet *invalidSourceAttributes = RKSetWithInvalidAttributesForEntity([attributes allKeys], [relationship entity]);
|
|
NSAssert([invalidSourceAttributes count] == 0, @"Cannot connect relationship: invalid attributes given for source entity '%@': %@", [[relationship entity] name], [[invalidSourceAttributes allObjects] componentsJoinedByString:@", "]);
|
|
NSSet *invalidDestinationAttributes = RKSetWithInvalidAttributesForEntity([attributes allValues], [relationship destinationEntity]);
|
|
NSAssert([invalidDestinationAttributes count] == 0, @"Cannot connect relationship: invalid attributes given for destination entity '%@': %@", [[relationship destinationEntity] name], [[invalidDestinationAttributes allObjects] componentsJoinedByString:@", "]);
|
|
|
|
self = [[RKForeignKeyConnectionDescription alloc] init];
|
|
if (self) {
|
|
self.relationship = relationship;
|
|
self.attributes = attributes;
|
|
}
|
|
return self;
|
|
}
|
|
|
|
- (id)initWithRelationship:(NSRelationshipDescription *)relationship keyPath:(NSString *)keyPath
|
|
{
|
|
NSParameterAssert(relationship);
|
|
NSParameterAssert(keyPath);
|
|
self = [[RKKeyPathConnectionDescription alloc] init];
|
|
if (self) {
|
|
self.relationship = relationship;
|
|
self.keyPath = keyPath;
|
|
}
|
|
return self;
|
|
}
|
|
|
|
- (id)init
|
|
{
|
|
if ([self class] == [RKConnectionDescription class]) {
|
|
@throw [NSException exceptionWithName:NSInternalInconsistencyException
|
|
reason:[NSString stringWithFormat:@"%@ Failed to call designated initializer. "
|
|
"Invoke initWithRelationship:sourceKeyPath:destinationKeyPath:matcher: instead.",
|
|
NSStringFromClass([self class])]
|
|
userInfo:nil];
|
|
}
|
|
return [super init];
|
|
}
|
|
|
|
- (id)copyWithZone:(NSZone *)zone
|
|
{
|
|
if ([self isForeignKeyConnection]) {
|
|
return [[[self class] allocWithZone:zone] initWithRelationship:self.relationship attributes:self.attributes];
|
|
} else if ([self isKeyPathConnection]) {
|
|
return [[[self class] allocWithZone:zone] initWithRelationship:self.relationship keyPath:self.keyPath];
|
|
}
|
|
|
|
return nil;
|
|
}
|
|
|
|
- (BOOL)isForeignKeyConnection
|
|
{
|
|
return NO;
|
|
}
|
|
|
|
- (BOOL)isKeyPathConnection
|
|
{
|
|
return NO;
|
|
}
|
|
|
|
@end
|
|
|
|
@implementation RKForeignKeyConnectionDescription
|
|
|
|
- (BOOL)isForeignKeyConnection
|
|
{
|
|
return YES;
|
|
}
|
|
|
|
- (NSString *)description
|
|
{
|
|
return [NSString stringWithFormat:@"<%@:%p connecting Relationship '%@' from Entity '%@' to Destination Entity '%@' with attributes=%@>",
|
|
NSStringFromClass([self class]), self, [self.relationship name], [[self.relationship entity] name],
|
|
[[self.relationship destinationEntity] name], self.attributes];
|
|
}
|
|
|
|
@end
|
|
|
|
@implementation RKKeyPathConnectionDescription
|
|
|
|
- (BOOL)isKeyPathConnection
|
|
{
|
|
return YES;
|
|
}
|
|
|
|
- (NSString *)description
|
|
{
|
|
return [NSString stringWithFormat:@"<%@:%p connecting Relationship '%@' of Entity '%@' with keyPath=%@>",
|
|
NSStringFromClass([self class]), self, [self.relationship name], [[self.relationship entity] name], self.keyPath];
|
|
}
|
|
|
|
@end
|