mirror of
https://github.com/zhigang1992/RestKit.git
synced 2026-04-23 04:20:21 +08:00
Drop RKConnectionMapping, cleanup. Add predicate support
This commit is contained in:
@@ -1,138 +0,0 @@
|
||||
//
|
||||
// RKConnectionMapping.h
|
||||
// RestKit
|
||||
//
|
||||
// Created by Charlie Savage on 5/15/12.
|
||||
// Copyright (c) 2009-2012 RestKit. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import <CoreData/CoreData.h>
|
||||
#import "RKMapping.h"
|
||||
#import "RKPropertyMapping.h"
|
||||
|
||||
@class RKConnectionMapping, RKDynamicMappingMatcher;
|
||||
@protocol RKManagedObjectCaching;
|
||||
|
||||
// Defines the rules for connecting relationsips
|
||||
/**
|
||||
Instructs RestKit to connect a relationship of the object being mapped to the
|
||||
appropriate target object(s). It does this by using the value of the object's
|
||||
fromKeyPath attribute to query instances of the target entity that have the
|
||||
same value in their toKeyPath attribute.
|
||||
|
||||
Note that connectRelationship runs *after* an object's attributes have been
|
||||
mapped and is dependent upon the results of those mappings. Also, connectRelationship
|
||||
will never create a new object - it simply looks up existing objects. In effect,
|
||||
connectRelationship allows foreign key relationships between managed objects
|
||||
to be automatically maintained from the server to the underlying Core Data object graph.
|
||||
|
||||
For example, given a Project object associated with a User, where the 'user' relationship is
|
||||
specified by a userID property on the managed object:
|
||||
|
||||
[mapping connectRelationship:@"user" withMapping:userMapping fromKeyPath:@"userId" toKeyPath:@"id"];
|
||||
|
||||
Will hydrate the 'user' association on the managed object with the object
|
||||
in the local object graph having the primary key specified in the managed object's
|
||||
userID property.
|
||||
|
||||
You can also do the reverse. Given a User object associated with a Project, with a
|
||||
'project' relationship:
|
||||
|
||||
[mapping connectRelationship:@"project" fromKeyPath:@"id" toKeyPath:@"userId" withMapping:projectMapping];
|
||||
*/
|
||||
//- (void)connectRelationship:(NSString *)relationshipName fromKeyPath:(NSString *)sourceKeyPath toKeyPath:(NSString *)destinationKeyPath withMapping:(RKMapping *)objectOrDynamicMapping DEPRECATED_ATTRIBUTE;
|
||||
|
||||
/**
|
||||
Conditionally connect a relationship of the object being mapped when the object being mapped has
|
||||
keyPath equal to a specified value.
|
||||
|
||||
For example, given a Project object associated with a User, where the 'admin' relationship is
|
||||
specified by a adminID property on the managed object:
|
||||
|
||||
[mapping connectRelationship:@"admin" fromKeyPath:@"adminId" toKeyPath:@"id" withMapping:userMapping whenValueOfKeyPath:@"userType" isEqualTo:@"Admin"];
|
||||
|
||||
Will hydrate the 'admin' association on the managed object with the object
|
||||
in the local object graph having the primary key specified in the managed object's
|
||||
userID property. Note that this connection will only occur when the Product's 'userType'
|
||||
property equals 'Admin'. In cases where no match occurs, the relationship connection is skipped.
|
||||
|
||||
@see connectRelationship:withObjectForPrimaryKeyAttribute:
|
||||
*/
|
||||
// - (void)connectRelationship:(NSString *)relationshipName fromKeyPath:(NSString *)sourceKeyPath toKeyPath:(NSString *)destinationKeyPath withMapping:(RKMapping *)objectOrDynamicMapping whenValueOfKeyPath:(NSString *)keyPath isEqualTo:(id)value DEPRECATED_ATTRIBUTE;
|
||||
/**
|
||||
Conditionally connect a relationship of the object being mapped when the object being mapped has
|
||||
block evaluate to YES. This variant is useful in cases where you want to execute an arbitrary
|
||||
block to determine whether or not to connect a relationship.
|
||||
|
||||
For example, given a Project object associated with a User, where the 'admin' relationship is
|
||||
specified by a adminID property on the managed object:
|
||||
|
||||
[mapping connectRelationship:@"admin" fromKeyPath:@"adminId" toKeyPath:@"adminID" withMapping:userMapping usingEvaluationBlock:^(id data) {
|
||||
return [User isAuthenticated];
|
||||
}];
|
||||
|
||||
Will hydrate the 'admin' association on the managed object with the object
|
||||
in the local object graph having the primary key specified in the managed object's
|
||||
userID property. Note that this connection will only occur when the provided block evalutes to YES.
|
||||
In cases where no match occurs, the relationship connection is skipped.
|
||||
|
||||
@see connectRelationship:withObjectForPrimaryKeyAttribute:
|
||||
*/
|
||||
|
||||
@interface RKConnectionMapping : RKPropertyMapping
|
||||
|
||||
@property (nonatomic, strong, readonly) NSRelationshipDescription *relationship;
|
||||
|
||||
@property (nonatomic, strong, readonly) RKDynamicMappingMatcher *matcher; // Can be nil
|
||||
|
||||
// Returns YES if the receiver describes a connection between entities that is established
|
||||
// using Foreign Key lookup via a Core Data Fetch Request
|
||||
- (BOOL)isForeignKeyConnection;
|
||||
|
||||
// Returns YES if the receiver describes a connection between entities that is established
|
||||
// using Key Path traversal of the receiver's object graph
|
||||
- (BOOL)isKeyPathConnection;
|
||||
/**
|
||||
Initializes the receiver with a relationship name, source key path, destination key path, mapping, and matcher.
|
||||
*/
|
||||
///**
|
||||
// Defines a mapping that is used to connect a source object relationship to
|
||||
// the appropriate target object(s).
|
||||
//
|
||||
// @param relationshipName The name of the relationship on the source object.
|
||||
// @param sourceKeyPath Specifies the path to an attribute on the source object that
|
||||
// contains the value that should be used to connect the relationship. This will generally
|
||||
// be a primary key or a foreign key value.
|
||||
// @param targetKeyPath Specifies the path to an attribute on the target object(s) that
|
||||
// must match the value of the sourceKeyPath attribute.
|
||||
// @param withMapping The mapping for the target object.
|
||||
//
|
||||
// @return A new instance of a RKObjectConnectionMapping.
|
||||
// */
|
||||
///**
|
||||
// Defines a mapping that is used to connect a source object relationship to
|
||||
// the appropriate target object(s). This is similar to mapping:fromKeyPath:toKeyPath:withMapping:
|
||||
// (@see mapping:fromKeyPath:toKeyPath:withMapping:) but adds in an additional matcher parameter
|
||||
// that can be used to filter source objects.
|
||||
//
|
||||
// @return A new instance of a RKObjectConnectionMapping.
|
||||
// */
|
||||
- (id)initWithRelationship:(NSRelationshipDescription *)relationship
|
||||
sourceKeyPath:(NSString *)sourceKeyPath
|
||||
destinationKeyPath:(NSString *)destinationKeyPath
|
||||
matcher:(RKDynamicMappingMatcher *)matcher;
|
||||
|
||||
@end
|
||||
@@ -1,135 +0,0 @@
|
||||
//
|
||||
// RKConnectionMapping.m
|
||||
// RestKit
|
||||
//
|
||||
// Created by Charlie Savage on 5/15/12.
|
||||
// Copyright (c) 2009-2012 RestKit. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
#import "RKConnectionMapping.h"
|
||||
#import "RKEntityMapping.h"
|
||||
#import "RKObjectManager.h"
|
||||
#import "RKManagedObjectCaching.h"
|
||||
#import "RKDynamicMappingMatcher.h"
|
||||
|
||||
// Provides support for connecting a relationship by
|
||||
@interface RKForeignKeyConnectionMapping : RKConnectionMapping
|
||||
@end
|
||||
|
||||
// Provides support for connecting a relationship by traversing the object graph
|
||||
@interface RKKeyPathConnectionMapping : RKConnectionMapping
|
||||
@end
|
||||
|
||||
@interface RKConnectionMapping ()
|
||||
@property (nonatomic, strong, readwrite) NSRelationshipDescription *relationship;
|
||||
@property (nonatomic, strong, readwrite) NSString *sourceKeyPath;
|
||||
@property (nonatomic, strong, readwrite) NSString *destinationKeyPath;
|
||||
@property (nonatomic, strong, readwrite) RKDynamicMappingMatcher *matcher;
|
||||
@end
|
||||
|
||||
@implementation RKConnectionMapping
|
||||
|
||||
- (Class)connectionMappingClassForRelationship:(NSRelationshipDescription *)relationship sourceKeyPath:(NSString *)sourceKeyPath destinationKeyPath:(NSString *)destinationKeyPath
|
||||
{
|
||||
NSEntityDescription *sourceEntity = relationship.entity;
|
||||
NSEntityDescription *destinationEntity = relationship.destinationEntity;
|
||||
|
||||
if ([[sourceEntity attributesByName] objectForKey:sourceKeyPath] && [[destinationEntity attributesByName] objectForKey:destinationKeyPath]) {
|
||||
return [RKForeignKeyConnectionMapping class];
|
||||
} else {
|
||||
return [RKKeyPathConnectionMapping class];
|
||||
}
|
||||
}
|
||||
|
||||
- (id)initWithRelationship:(NSRelationshipDescription *)relationship sourceKeyPath:(NSString *)sourceKeyPath destinationKeyPath:(NSString *)destinationKeyPath matcher:(RKDynamicMappingMatcher *)matcher
|
||||
{
|
||||
NSParameterAssert(relationship);
|
||||
NSParameterAssert(sourceKeyPath);
|
||||
|
||||
Class connectionClass = [self connectionMappingClassForRelationship:relationship sourceKeyPath:sourceKeyPath destinationKeyPath:destinationKeyPath];
|
||||
self = [[connectionClass alloc] init];
|
||||
if (self) {
|
||||
self.relationship = relationship;
|
||||
self.sourceKeyPath = sourceKeyPath;
|
||||
self.destinationKeyPath = destinationKeyPath ?: [relationship name];
|
||||
self.matcher = matcher;
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (id)init
|
||||
{
|
||||
if ([self class] == [RKConnectionMapping 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
|
||||
{
|
||||
return [[[self class] allocWithZone:zone] initWithRelationship:self.relationship
|
||||
sourceKeyPath:self.sourceKeyPath
|
||||
destinationKeyPath:self.destinationKeyPath
|
||||
matcher:self.matcher];
|
||||
}
|
||||
|
||||
- (BOOL)isForeignKeyConnection
|
||||
{
|
||||
return NO;
|
||||
}
|
||||
|
||||
- (BOOL)isKeyPathConnection
|
||||
{
|
||||
return NO;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
|
||||
@implementation RKForeignKeyConnectionMapping
|
||||
|
||||
- (BOOL)isForeignKeyConnection
|
||||
{
|
||||
return YES;
|
||||
}
|
||||
|
||||
- (NSString *)description
|
||||
{
|
||||
return [NSString stringWithFormat:@"<%@:%p connecting Relationship '%@' from Entity '%@' with sourceKeyPath=%@ to Destination Entity '%@' with destinationKeyPath=%@>",
|
||||
NSStringFromClass([self class]), self, self.relationship.name, self.relationship.entity.name, self.sourceKeyPath,
|
||||
self.relationship.destinationEntity.name, self.self.destinationKeyPath];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@implementation RKKeyPathConnectionMapping
|
||||
|
||||
- (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.sourceKeyPath];
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in New Issue
Block a user