Created
October 5, 2012 15:57
-
-
Save lukeredpath/3840680 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// RKObjectMapping+TransformationMapping.h | |
// OtherScreen | |
// | |
// Created by Luke Redpath on 05/10/2012. | |
// Copyright (c) 2012 Mark Rickert. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
#import <RestKit/RestKit.h> | |
typedef NSString* (^RKObjectMappingSourceKeyTransformationBlock)(NSString *sourceKey); | |
/* This category allows mappings to be configured using a block to transform | |
the source key into the destination key. | |
The use case for this is when your source keys map to your destination keys using | |
a common pattern, e.g. snake_case attributes mapping to Objective-C style lowerCamelCase | |
attributes. | |
Configuring these kind of mappings is tedious as the transformation can be | |
done programatically, which is what this category allows you to do. | |
*/ | |
@interface RKObjectMapping (TransformationMapping) | |
/* Adds attribute mappings for the given source keys, applying the transformation | |
block to each source key in turn to derive the destination attribute key. | |
Simply pass in a block, and within that block, apply whatever string transformation | |
makes sense to the provided source key and return a destination key. | |
If your block returns nil, the key will be skipped. | |
*/ | |
- (void)addAttributeMappingsFromArrayOfSourceKeys:(NSArray *)keys | |
transformSourceKeysToDestinationKeysUsingBlock:(RKObjectMappingSourceKeyTransformationBlock)block; | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// RKObjectMapping+TransformationMapping.m | |
// OtherScreen | |
// | |
// Created by Luke Redpath on 05/10/2012. | |
// | |
#import "RKObjectMapping+TransformationMapping.h" | |
@implementation RKObjectMapping (TransformationMapping) | |
- (void)addAttributeMappingsFromArrayOfSourceKeys:(NSArray *)keys | |
transformSourceKeysToDestinationKeysUsingBlock:(RKObjectMappingSourceKeyTransformationBlock)block | |
{ | |
NSMutableDictionary *mappings = [NSMutableDictionary dictionaryWithCapacity:keys.count]; | |
[keys enumerateObjectsUsingBlock:^(id sourceKey, NSUInteger idx, BOOL *stop) { | |
NSString *destinationKey = block(sourceKey); | |
if (destinationKey) { | |
[mappings setObject:destinationKey forKey:sourceKey]; | |
} | |
}]; | |
[self addAttributeMappingsFromDictionary:mappings]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment