Add objectMapping identity attribute on RKPropertyMapping to allow one to traverse the mapping graph from an attribute/relationship back to the parent mapping

This commit is contained in:
Blake Watters
2012-10-18 14:37:52 -04:00
parent df55f0e524
commit 5684f7ab89
3 changed files with 35 additions and 1 deletions

View File

@@ -33,6 +33,10 @@ static NSUInteger RKObjectMappingMaximumInverseMappingRecursionDepth = 100;
// Private declaration
NSDate *RKDateFromStringWithFormatters(NSString *dateString, NSArray *formatters);
@interface RKPropertyMapping ()
@property (nonatomic, weak, readwrite) RKObjectMapping *objectMapping;
@end
@interface RKObjectMapping ()
@property (nonatomic, weak, readwrite) Class objectClass;
@property (nonatomic, strong) NSMutableArray *mutablePropertyMappings;
@@ -146,6 +150,8 @@ NSDate *RKDateFromStringWithFormatters(NSString *dateString, NSArray *formatters
NSAssert1([[self mappedKeyPaths] containsObject:propertyMapping.destinationKeyPath] == NO,
@"Unable to add mapping for keyPath %@, one already exists...", propertyMapping.destinationKeyPath);
NSAssert(self.mutablePropertyMappings, @"self.mutablePropertyMappings is nil");
NSAssert(propertyMapping.objectMapping == nil, @"Cannot add a property mapping that already exists in another mapping.");
propertyMapping.objectMapping = self;
[self.mutablePropertyMappings addObject:propertyMapping];
}
@@ -212,7 +218,10 @@ NSDate *RKDateFromStringWithFormatters(NSString *dateString, NSArray *formatters
- (void)removePropertyMapping:(RKPropertyMapping *)attributeOrRelationshipMapping
{
[self.mutablePropertyMappings removeObject:attributeOrRelationshipMapping];
if ([self.mutablePropertyMappings containsObject:attributeOrRelationshipMapping]) {
attributeOrRelationshipMapping.objectMapping = nil;
[self.mutablePropertyMappings removeObject:attributeOrRelationshipMapping];
}
}
- (RKObjectMapping *)inverseMappingAtDepth:(NSInteger)depth

View File

@@ -20,11 +20,22 @@
#import <Foundation/Foundation.h>
@class RKObjectMapping;
/**
`RKPropertyMapping` is an abstract class for describing the properties being mapped within an `RKObjectMapping` or `RKEntityMapping` object. It defines the common interface for its concrete subclasses `RKAttributeMapping` and `RKRelationshipMapping`. Each property mapping defines a single transformation from a source key path (often in the deserialized representation of a JSON or XML document) to a destination key path (typically on a target object).
*/
@interface RKPropertyMapping : NSObject <NSCopying>
///------------------------------------------
/// @name Accessing the Parent Object Mapping
///------------------------------------------
/**
Returns the object mapping the receiver is added to.
*/
@property (nonatomic, weak, readonly) RKObjectMapping *objectMapping;
///-----------------------------------------------------
/// @name Accessing the Source and Destination Key Paths
///-----------------------------------------------------

View File

@@ -5,10 +5,24 @@
// Created by Blake Watters on 8/27/12.
// Copyright (c) 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 "RKPropertyMapping.h"
@interface RKPropertyMapping ()
// Synthesize as read/write to allow assignment in `RKObjectMapping`
@property (nonatomic, weak, readwrite) RKObjectMapping *objectMapping;
@property (nonatomic, strong, readwrite) NSString *sourceKeyPath;
@property (nonatomic, strong, readwrite) NSString *destinationKeyPath;
@end