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:
Blake Watters
2012-12-27 22:12:40 -05:00
parent bbfec220ed
commit 5c21e52829
19 changed files with 466 additions and 273 deletions

View File

@@ -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;