mirror of
https://github.com/zhigang1992/RestKit.git
synced 2026-04-23 20:31:13 +08:00
Update Dynamic Mapping API's to match the rest of the 0.20.x style. Introduce support for predicate based dynamic matching.
* Rename RKDynamicMappingMatcher to RKObjectMappingMatcher since it is not strictly coupled to dynamic mapping and works with object mappings. * Rework matchers into using a class cluster style to enable flexible subclassing to introduce additional matchers.
This commit is contained in:
@@ -19,7 +19,7 @@
|
||||
//
|
||||
|
||||
#import "RKDynamicMapping.h"
|
||||
#import "RKDynamicMappingMatcher.h"
|
||||
#import "RKObjectMappingMatcher.h"
|
||||
#import "RKLog.h"
|
||||
|
||||
// Set Logging Component
|
||||
@@ -27,8 +27,8 @@
|
||||
#define RKLogComponent RKlcl_cRestKitObjectMapping
|
||||
|
||||
@interface RKDynamicMapping ()
|
||||
@property (nonatomic, strong) NSMutableArray *matchers;
|
||||
@property (nonatomic, copy) RKDynamicMappingDelegateBlock objectMappingForRepresentationBlock;
|
||||
@property (nonatomic, strong) NSMutableArray *mutableMatchers;
|
||||
@property (nonatomic, copy) RKObjectMapping *(^objectMappingForRepresentationBlock)(id representation);
|
||||
@end
|
||||
|
||||
@implementation RKDynamicMapping
|
||||
@@ -37,22 +37,37 @@
|
||||
{
|
||||
self = [super init];
|
||||
if (self) {
|
||||
self.matchers = [NSMutableArray new];
|
||||
self.mutableMatchers = [NSMutableArray new];
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (NSArray *)objectMappings
|
||||
- (NSArray *)matchers
|
||||
{
|
||||
return [self.matchers valueForKey:@"objectMapping"];
|
||||
return [self.mutableMatchers copy];
|
||||
}
|
||||
|
||||
- (void)setObjectMapping:(RKObjectMapping *)objectMapping whenValueOfKeyPath:(NSString *)keyPath isEqualTo:(id)expectedValue
|
||||
- (NSArray *)objectMappings
|
||||
{
|
||||
RKLogDebug(@"Adding dynamic object mapping for key '%@' with value '%@' to destination class: %@", keyPath, expectedValue, NSStringFromClass(objectMapping.objectClass));
|
||||
RKDynamicMappingMatcher *matcher = [[RKDynamicMappingMatcher alloc] initWithKeyPath:keyPath expectedValue:expectedValue objectMapping:objectMapping];
|
||||
[_matchers addObject:matcher];
|
||||
return [self.mutableMatchers valueForKey:@"objectMapping"];
|
||||
}
|
||||
|
||||
- (void)addMatcher:(RKObjectMappingMatcher *)matcher
|
||||
{
|
||||
NSParameterAssert(matcher);
|
||||
if ([self.mutableMatchers containsObject:matcher]) {
|
||||
[self.mutableMatchers removeObject:matcher];
|
||||
[self.mutableMatchers insertObject:matcher atIndex:0];
|
||||
} else {
|
||||
[self.mutableMatchers addObject:matcher];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)removeMatcher:(RKObjectMappingMatcher *)matcher
|
||||
{
|
||||
NSParameterAssert(matcher);
|
||||
[self.mutableMatchers removeObject:matcher];
|
||||
}
|
||||
|
||||
- (RKObjectMapping *)objectMappingForRepresentation:(id)representation
|
||||
@@ -62,7 +77,7 @@
|
||||
RKLogTrace(@"Performing dynamic object mapping for object representation: %@", representation);
|
||||
|
||||
// Consult the declarative matchers first
|
||||
for (RKDynamicMappingMatcher *matcher in _matchers) {
|
||||
for (RKObjectMappingMatcher *matcher in self.mutableMatchers) {
|
||||
if ([matcher matches:representation]) {
|
||||
RKLogTrace(@"Found declarative match for matcher: %@.", matcher);
|
||||
return matcher.objectMapping;
|
||||
|
||||
Reference in New Issue
Block a user