Skip to content

Commit

Permalink
Merge pull request #24 from aryaxt/ExcludeKeysInObjectToDictionaryMap…
Browse files Browse the repository at this point in the history
…ping

Exclude keys form being mapped into a dictionary
  • Loading branch information
aryaxt committed Apr 7, 2015
2 parents d13f5b2 + 53cc688 commit a876bb8
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 2 deletions.
4 changes: 2 additions & 2 deletions OCMapper.podspec
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
Pod::Spec.new do |s|
s.platform = :ios, '5.0'
s.name = 'OCMapper'
s.version = '1.5'
s.version = '1.6'
s.summary = 'NSDictionary to NSObject Mapper'
s.homepage = 'https://github.com/aryaxt/OCMapper'
s.license = {
:type => 'MIT',
:file => 'License.txt'
}
s.author = {'Aryan Ghassemi' => 'https://github.com/aryaxt/OCMapper'}
s.source = {:git => 'https://github.com/aryaxt/OCMapper.git', :tag => '1.5'}
s.source = {:git => 'https://github.com/aryaxt/OCMapper.git', :tag => '1.6'}
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}'
s.framework = 'Foundation'
s.requires_arc = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@
*/
- (void)mapFromPropertyKey:(NSString *)propertyKey toDictionaryKey:(NSString *)dictionaryKey forClass:(Class)class withTransformer:(MappingTransformer)transformer;

/**
* Set keys to be excluded when mapping model to a dictionary
*
* @param class Class to be assign mapping to
* @param keys NSArray of NSStrings, include properties to be excluded when mapping
*/
- (void)excludeMappingForClass:(Class)class withKeys:(NSArray *)keys;

/**
* Set dateformatter to be used for converting a dictionary to a model object
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ @interface InCodeMappingProvider()
@property (nonatomic, strong) NSMutableDictionary *inverseMappingDictionary;
@property (nonatomic, strong) NSMutableDictionary *dateFormatterDictionary;
@property (nonatomic, strong) NSMutableDictionary *inverseDateFormatterDictionary;
@property (nonatomic, strong) NSMutableDictionary *excludeKeysDictionary;
@end

@implementation InCodeMappingProvider
Expand All @@ -51,6 +52,7 @@ - (id)init
self.inverseMappingDictionary = [NSMutableDictionary dictionary];
self.dateFormatterDictionary = [NSMutableDictionary dictionary];
self.inverseDateFormatterDictionary = [NSMutableDictionary dictionary];
self.excludeKeysDictionary = [NSMutableDictionary dictionary];
}

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

- (void)excludeMappingForClass:(Class)class withKeys:(NSArray *)keys
{
[self.excludeKeysDictionary setObject:keys forKey:NSStringFromClass(class)];
}

- (void)setDateFormatter:(NSDateFormatter *)dateFormatter forPropertyKey:(NSString *)property andClass:(Class)class
{
NSString *key = [self uniqueKeyForClass:class andKey:property];
Expand Down Expand Up @@ -143,4 +150,9 @@ - (NSDateFormatter *)dateFormatterForClass:(Class)class andDictionaryKey:(NSStri
return [self.dateFormatterDictionary objectForKey:key];
}

- (NSArray *)excludedKeysForClass:(Class)class
{
return [self.excludeKeysDictionary objectForKey:NSStringFromClass(class)];
}

@end
1 change: 1 addition & 0 deletions OCMapper/Source/Mapping Provider/MappingProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,6 @@
- (ObjectMappingInfo *)mappingInfoForClass:(Class)class andPropertyKey:(NSString *)key;
- (NSDateFormatter *)dateFormatterForClass:(Class)class andPropertyKey:(NSString *)key;
- (NSDateFormatter *)dateFormatterForClass:(Class)class andDictionaryKey:(NSString *)key;
- (NSArray *)excludedKeysForClass:(Class)class;

@end
6 changes: 6 additions & 0 deletions OCMapper/Source/ObjectMapper.m
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,17 @@ - (id)processDictionaryFromObject:(NSObject *)object
{
unsigned int outCount, i;
objc_property_t *properties = class_copyPropertyList(currentClass, &outCount);
NSArray *excludedKeys = [self.mappingProvider excludedKeysForClass:currentClass];

for (i = 0; i < outCount; i++)
{
objc_property_t property = properties[i];
NSString *originalPropertyName = [NSString stringWithUTF8String:property_getName(property)];

if (excludedKeys && [excludedKeys containsObject:originalPropertyName]) {
continue;
}

Class class = NSClassFromString([self typeForProperty:originalPropertyName andClass:[object class]]);
id propertyValue = [object valueForKey:(NSString *)originalPropertyName];

Expand Down
40 changes: 40 additions & 0 deletions OCMapperTests/ObjectMapperTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,46 @@ - (void)testShouldNotAutomaticallyGenerateInverseMapping
XCTAssertNil(info);
}

- (void)testShouldMapArrayOfStringFromDictionaryToObject
{
NSMutableDictionary *userDictionary = [NSMutableDictionary dictionary];
[userDictionary setObject:@[@"keyword1", @2] forKey:@"randomKeywords"];

User *user = [self.mapper objectFromSource:userDictionary toInstanceOfClass:[User class]];
XCTAssertTrue(user.randomKeywords.count == 2);
XCTAssertTrue([user.randomKeywords[0] isEqualToString:@"keyword1"]);
XCTAssertTrue([user.randomKeywords[1] isEqualToNumber:@2]);
}

- (void)testShouldMapArrayOfStringFromObjectToDictionary
{
User *user = [[User alloc] init];
user.randomKeywords = @[@"keyword1", @2].mutableCopy;

NSDictionary *dictionary = [self.mapper dictionaryFromObject:user];
NSArray *array = [dictionary objectForKey:@"randomKeywords"];

XCTAssertTrue(array.count == 2);
XCTAssertTrue([array[0] isEqualToString:@"keyword1"]);
XCTAssertTrue([array[1] isEqualToNumber:@2]);
}

- (void)testShouldNotMapExcludedKeys {
User *user = [[User alloc] init];
user.firstName = @"f";
user.lastName = @"l";
user.age = @28;
user.dateOfBirth = [NSDate date];

[self.mappingProvider excludeMappingForClass:User.class withKeys:@[@"age", @"lastName"]];

NSDictionary *dictionary = [self.mapper dictionaryFromObject:user];
XCTAssertNotNil(dictionary[@"firstName"]);
XCTAssertNotNil(dictionary[@"dateOfBirth"]);
XCTAssertNil(dictionary[@"age"]);
XCTAssertNil(dictionary[@"lastName"]);
}

- (void)testObjectInstanceProviderShouldReturnTrueForNSObjectSubclasses
{
XCTAssertTrue([self.instanceProvider canHandleClass:User.class]);
Expand Down

0 comments on commit a876bb8

Please sign in to comment.