Skip to content

Commit a876bb8

Browse files
committed
Merge pull request #24 from aryaxt/ExcludeKeysInObjectToDictionaryMapping
Exclude keys form being mapped into a dictionary
2 parents d13f5b2 + 53cc688 commit a876bb8

File tree

6 files changed

+69
-2
lines changed

6 files changed

+69
-2
lines changed

OCMapper.podspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
Pod::Spec.new do |s|
22
s.platform = :ios, '5.0'
33
s.name = 'OCMapper'
4-
s.version = '1.5'
4+
s.version = '1.6'
55
s.summary = 'NSDictionary to NSObject Mapper'
66
s.homepage = 'https://github.com/aryaxt/OCMapper'
77
s.license = {
88
:type => 'MIT',
99
:file => 'License.txt'
1010
}
1111
s.author = {'Aryan Ghassemi' => 'https://github.com/aryaxt/OCMapper'}
12-
s.source = {:git => 'https://github.com/aryaxt/OCMapper.git', :tag => '1.5'}
12+
s.source = {:git => 'https://github.com/aryaxt/OCMapper.git', :tag => '1.6'}
1313
s.source_files = 'OCMapper/Source/*.{h,m}','OCMapper/Source/Categories/*.{h,m}','OCMapper/Source/Logging Provider/*.{h,m}','OCMapper/Source/Instance Provider/*.{h,m}','OCMapper/Source/Mapping Provider/*.{h,m}','OCMapper/Source/Mapping Provider/In Code Mapping/*.{h,m}','OCMapper/Source/Mapping Provider/PLIST Mapping/*.{h,m}','OCMapper/Source/Mapping Provider/XML Mapping/*.{h,m}'
1414
s.framework = 'Foundation'
1515
s.requires_arc = true

OCMapper/Source/Mapping Provider/In Code Mapping/InCodeMappingProvider.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,14 @@
8888
*/
8989
- (void)mapFromPropertyKey:(NSString *)propertyKey toDictionaryKey:(NSString *)dictionaryKey forClass:(Class)class withTransformer:(MappingTransformer)transformer;
9090

91+
/**
92+
* Set keys to be excluded when mapping model to a dictionary
93+
*
94+
* @param class Class to be assign mapping to
95+
* @param keys NSArray of NSStrings, include properties to be excluded when mapping
96+
*/
97+
- (void)excludeMappingForClass:(Class)class withKeys:(NSArray *)keys;
98+
9199
/**
92100
* Set dateformatter to be used for converting a dictionary to a model object
93101
*

OCMapper/Source/Mapping Provider/In Code Mapping/InCodeMappingProvider.m

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ @interface InCodeMappingProvider()
3434
@property (nonatomic, strong) NSMutableDictionary *inverseMappingDictionary;
3535
@property (nonatomic, strong) NSMutableDictionary *dateFormatterDictionary;
3636
@property (nonatomic, strong) NSMutableDictionary *inverseDateFormatterDictionary;
37+
@property (nonatomic, strong) NSMutableDictionary *excludeKeysDictionary;
3738
@end
3839

3940
@implementation InCodeMappingProvider
@@ -51,6 +52,7 @@ - (id)init
5152
self.inverseMappingDictionary = [NSMutableDictionary dictionary];
5253
self.dateFormatterDictionary = [NSMutableDictionary dictionary];
5354
self.inverseDateFormatterDictionary = [NSMutableDictionary dictionary];
55+
self.excludeKeysDictionary = [NSMutableDictionary dictionary];
5456
}
5557

5658
return self;
@@ -94,6 +96,11 @@ - (void)mapFromPropertyKey:(NSString *)propertyKey toDictionaryKey:(NSString *)d
9496
[self.inverseMappingDictionary setObject:info forKey:key];
9597
}
9698

99+
- (void)excludeMappingForClass:(Class)class withKeys:(NSArray *)keys
100+
{
101+
[self.excludeKeysDictionary setObject:keys forKey:NSStringFromClass(class)];
102+
}
103+
97104
- (void)setDateFormatter:(NSDateFormatter *)dateFormatter forPropertyKey:(NSString *)property andClass:(Class)class
98105
{
99106
NSString *key = [self uniqueKeyForClass:class andKey:property];
@@ -143,4 +150,9 @@ - (NSDateFormatter *)dateFormatterForClass:(Class)class andDictionaryKey:(NSStri
143150
return [self.dateFormatterDictionary objectForKey:key];
144151
}
145152

153+
- (NSArray *)excludedKeysForClass:(Class)class
154+
{
155+
return [self.excludeKeysDictionary objectForKey:NSStringFromClass(class)];
156+
}
157+
146158
@end

OCMapper/Source/Mapping Provider/MappingProvider.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,6 @@
3434
- (ObjectMappingInfo *)mappingInfoForClass:(Class)class andPropertyKey:(NSString *)key;
3535
- (NSDateFormatter *)dateFormatterForClass:(Class)class andPropertyKey:(NSString *)key;
3636
- (NSDateFormatter *)dateFormatterForClass:(Class)class andDictionaryKey:(NSString *)key;
37+
- (NSArray *)excludedKeysForClass:(Class)class;
3738

3839
@end

OCMapper/Source/ObjectMapper.m

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,11 +182,17 @@ - (id)processDictionaryFromObject:(NSObject *)object
182182
{
183183
unsigned int outCount, i;
184184
objc_property_t *properties = class_copyPropertyList(currentClass, &outCount);
185+
NSArray *excludedKeys = [self.mappingProvider excludedKeysForClass:currentClass];
185186

186187
for (i = 0; i < outCount; i++)
187188
{
188189
objc_property_t property = properties[i];
189190
NSString *originalPropertyName = [NSString stringWithUTF8String:property_getName(property)];
191+
192+
if (excludedKeys && [excludedKeys containsObject:originalPropertyName]) {
193+
continue;
194+
}
195+
190196
Class class = NSClassFromString([self typeForProperty:originalPropertyName andClass:[object class]]);
191197
id propertyValue = [object valueForKey:(NSString *)originalPropertyName];
192198

OCMapperTests/ObjectMapperTests.m

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,46 @@ - (void)testShouldNotAutomaticallyGenerateInverseMapping
454454
XCTAssertNil(info);
455455
}
456456

457+
- (void)testShouldMapArrayOfStringFromDictionaryToObject
458+
{
459+
NSMutableDictionary *userDictionary = [NSMutableDictionary dictionary];
460+
[userDictionary setObject:@[@"keyword1", @2] forKey:@"randomKeywords"];
461+
462+
User *user = [self.mapper objectFromSource:userDictionary toInstanceOfClass:[User class]];
463+
XCTAssertTrue(user.randomKeywords.count == 2);
464+
XCTAssertTrue([user.randomKeywords[0] isEqualToString:@"keyword1"]);
465+
XCTAssertTrue([user.randomKeywords[1] isEqualToNumber:@2]);
466+
}
467+
468+
- (void)testShouldMapArrayOfStringFromObjectToDictionary
469+
{
470+
User *user = [[User alloc] init];
471+
user.randomKeywords = @[@"keyword1", @2].mutableCopy;
472+
473+
NSDictionary *dictionary = [self.mapper dictionaryFromObject:user];
474+
NSArray *array = [dictionary objectForKey:@"randomKeywords"];
475+
476+
XCTAssertTrue(array.count == 2);
477+
XCTAssertTrue([array[0] isEqualToString:@"keyword1"]);
478+
XCTAssertTrue([array[1] isEqualToNumber:@2]);
479+
}
480+
481+
- (void)testShouldNotMapExcludedKeys {
482+
User *user = [[User alloc] init];
483+
user.firstName = @"f";
484+
user.lastName = @"l";
485+
user.age = @28;
486+
user.dateOfBirth = [NSDate date];
487+
488+
[self.mappingProvider excludeMappingForClass:User.class withKeys:@[@"age", @"lastName"]];
489+
490+
NSDictionary *dictionary = [self.mapper dictionaryFromObject:user];
491+
XCTAssertNotNil(dictionary[@"firstName"]);
492+
XCTAssertNotNil(dictionary[@"dateOfBirth"]);
493+
XCTAssertNil(dictionary[@"age"]);
494+
XCTAssertNil(dictionary[@"lastName"]);
495+
}
496+
457497
- (void)testObjectInstanceProviderShouldReturnTrueForNSObjectSubclasses
458498
{
459499
XCTAssertTrue([self.instanceProvider canHandleClass:User.class]);

0 commit comments

Comments
 (0)