Skip to content

Commit

Permalink
- Added ability to map properties in super classes
Browse files Browse the repository at this point in the history
  • Loading branch information
aryaxt committed Dec 21, 2013
1 parent 25ac2c0 commit ed5f29f
Show file tree
Hide file tree
Showing 11 changed files with 132 additions and 17 deletions.
12 changes: 12 additions & 0 deletions OCMapper.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
15E2B989171BEAEB00526C77 /* ObjectMapper.m in Sources */ = {isa = PBXBuildFile; fileRef = 15E2B97A171BEAEB00526C77 /* ObjectMapper.m */; };
15E2B98A171BEAEB00526C77 /* ObjectMappingInfo.m in Sources */ = {isa = PBXBuildFile; fileRef = 15E2B97C171BEAEB00526C77 /* ObjectMappingInfo.m */; };
15E2B992171BEAF400526C77 /* ObjectMapperTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 15E2B98F171BEAF400526C77 /* ObjectMapperTests.m */; };
15EA38021866102D005CCEA0 /* SpecialUser.m in Sources */ = {isa = PBXBuildFile; fileRef = 15EA38011866102D005CCEA0 /* SpecialUser.m */; };
15EA380518661548005CCEA0 /* CDSpecialUser.m in Sources */ = {isa = PBXBuildFile; fileRef = 15EA380418661548005CCEA0 /* CDSpecialUser.m */; };
15EF0535172783F70041358D /* InCodeMappingProvider.m in Sources */ = {isa = PBXBuildFile; fileRef = 15EF052F172783F70041358D /* InCodeMappingProvider.m */; };
/* End PBXBuildFile section */

Expand Down Expand Up @@ -168,6 +170,10 @@
15E2B98F171BEAF400526C77 /* ObjectMapperTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ObjectMapperTests.m; sourceTree = "<group>"; };
15E2B990171BEAF400526C77 /* OCMapperTests-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "OCMapperTests-Info.plist"; sourceTree = "<group>"; };
15E2B994171BED2400526C77 /* OCMapper.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OCMapper.h; sourceTree = "<group>"; };
15EA38001866102D005CCEA0 /* SpecialUser.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SpecialUser.h; sourceTree = "<group>"; };
15EA38011866102D005CCEA0 /* SpecialUser.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SpecialUser.m; sourceTree = "<group>"; };
15EA380318661548005CCEA0 /* CDSpecialUser.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CDSpecialUser.h; sourceTree = "<group>"; };
15EA380418661548005CCEA0 /* CDSpecialUser.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CDSpecialUser.m; sourceTree = "<group>"; };
15EF052E172783F70041358D /* InCodeMappingProvider.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = InCodeMappingProvider.h; sourceTree = "<group>"; };
15EF052F172783F70041358D /* InCodeMappingProvider.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = InCodeMappingProvider.m; sourceTree = "<group>"; };
/* End PBXFileReference section */
Expand Down Expand Up @@ -273,6 +279,8 @@
157B309A171E5D32005AAB02 /* CDPost.h */,
157B309B171E5D32005AAB02 /* CDPost.m */,
157B307D171E3714005AAB02 /* OCMapper.xcdatamodeld */,
15EA380318661548005CCEA0 /* CDSpecialUser.h */,
15EA380418661548005CCEA0 /* CDSpecialUser.m */,
);
path = "Core Data";
sourceTree = "<group>";
Expand Down Expand Up @@ -399,6 +407,8 @@
15E2B971171BEAEB00526C77 /* User.m */,
157B316217238C08005AAB02 /* ObjectMappingConfig.xml */,
15371EBF1727879700A508F4 /* ObjectMappingConfig.plist */,
15EA38001866102D005CCEA0 /* SpecialUser.h */,
15EA38011866102D005CCEA0 /* SpecialUser.m */,
);
path = Models;
sourceTree = "<group>";
Expand Down Expand Up @@ -571,8 +581,10 @@
1502B37117CD94EA00C095DE /* SampleViewController.m in Sources */,
1502B38A17CD985300C095DE /* AFHTTPClient.m in Sources */,
1502B38B17CD985300C095DE /* AFHTTPRequestOperation.m in Sources */,
15EA380518661548005CCEA0 /* CDSpecialUser.m in Sources */,
1502B38C17CD985300C095DE /* AFImageRequestOperation.m in Sources */,
1502B38D17CD985300C095DE /* AFJSONRequestOperation.m in Sources */,
15EA38021866102D005CCEA0 /* SpecialUser.m in Sources */,
1502B38E17CD985300C095DE /* AFNetworkActivityIndicatorManager.m in Sources */,
1502B38F17CD985300C095DE /* AFPropertyListRequestOperation.m in Sources */,
1502B39017CD985300C095DE /* AFURLConnectionOperation.m in Sources */,
Expand Down
15 changes: 15 additions & 0 deletions OCMapper/Models/Core Data/CDSpecialUser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// CDSpecialUser.h
// OCMapper
//
// Created by Aryan Gh on 12/21/13.
// Copyright (c) 2013 Aryan Ghassemi. All rights reserved.
//

#import "CDUser.h"

@interface CDSpecialUser : CDUser

@property (nonatomic, strong) NSString *power;

@end
14 changes: 14 additions & 0 deletions OCMapper/Models/Core Data/CDSpecialUser.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// CDSpecialUser.m
// OCMapper
//
// Created by Aryan Gh on 12/21/13.
// Copyright (c) 2013 Aryan Ghassemi. All rights reserved.
//

#import "CDSpecialUser.h"

@implementation CDSpecialUser
@dynamic power;

@end
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<model name="" userDefinedModelVersionIdentifier="" type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="1810" systemVersion="12D78" minimumToolsVersion="Xcode 4.3" macOSVersion="Automatic" iOSVersion="Automatic">
<model userDefinedModelVersionIdentifier="" type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="3400" systemVersion="13A603" minimumToolsVersion="Xcode 4.3" macOSVersion="Automatic" iOSVersion="Automatic">
<entity name="CDAddress" representedClassName="CDAddress" syncable="YES">
<attribute name="city" optional="YES" attributeType="String" syncable="YES"/>
<attribute name="country" optional="YES" attributeType="String" syncable="YES"/>
Expand All @@ -10,6 +10,9 @@
<attribute name="title" optional="YES" attributeType="String" syncable="YES"/>
<relationship name="user" optional="YES" minCount="1" maxCount="1" deletionRule="Nullify" destinationEntity="CDUser" inverseName="posts" inverseEntity="CDUser" syncable="YES"/>
</entity>
<entity name="CDSpecialUser" representedClassName="CDSpecialUser" parentEntity="CDUser" syncable="YES">
<attribute name="power" optional="YES" attributeType="String" syncable="YES"/>
</entity>
<entity name="CDUser" representedClassName="CDUser" syncable="YES">
<attribute name="age" optional="YES" attributeType="Integer 16" defaultValueString="0" syncable="YES"/>
<attribute name="dateOfBirth" optional="YES" attributeType="Date" syncable="YES"/>
Expand All @@ -19,8 +22,9 @@
<relationship name="posts" optional="YES" toMany="YES" deletionRule="Nullify" destinationEntity="CDPost" inverseName="user" inverseEntity="CDPost" syncable="YES"/>
</entity>
<elements>
<element name="CDUser" positionX="-63" positionY="-27" width="128" height="133"/>
<element name="CDAddress" positionX="-288" positionY="-10" width="128" height="88"/>
<element name="CDPost" positionX="117" positionY="-9" width="128" height="88"/>
<element name="CDAddress" positionX="0" positionY="0" width="0" height="0"/>
<element name="CDPost" positionX="0" positionY="0" width="0" height="0"/>
<element name="CDUser" positionX="0" positionY="0" width="0" height="0"/>
<element name="CDSpecialUser" positionX="0" positionY="0" width="0" height="0"/>
</elements>
</model>
15 changes: 15 additions & 0 deletions OCMapper/Models/SpecialUser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// SpecialUser.h
// OCMapper
//
// Created by Aryan Gh on 12/21/13.
// Copyright (c) 2013 Aryan Ghassemi. All rights reserved.
//

#import "User.h"

@interface SpecialUser : User

@property (nonatomic, strong) NSString *power;

@end
13 changes: 13 additions & 0 deletions OCMapper/Models/SpecialUser.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// SpecialUser.m
// OCMapper
//
// Created by Aryan Gh on 12/21/13.
// Copyright (c) 2013 Aryan Ghassemi. All rights reserved.
//

#import "SpecialUser.h"

@implementation SpecialUser

@end
2 changes: 1 addition & 1 deletion OCMapper/Sample/Service Layer/OCMapperConfig.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

@implementation OCMapperConfig

// We call this from appDelegate
// We call this from appDelegate when application loads

+ (void)configure
{
Expand Down
31 changes: 21 additions & 10 deletions OCMapper/Source/Instance Provider/ObjectInstanceProvider.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,34 @@ - (id)emptyInstanceOfCollectionObject
- (NSString *)propertyNameForObject:(NSObject *)object byCaseInsensitivePropertyName:(NSString *)caseInsensitivePropertyName
{
NSString *result = nil;
unsigned int outCount, i;
objc_property_t *properties = class_copyPropertyList([object class], &outCount);
Class currentClass = [object class];

for (i = 0; i < outCount; i++)
while (currentClass && currentClass != [NSObject class])
{
objc_property_t property = properties[i];
NSString *propertyName = [NSString stringWithUTF8String:property_getName(property)];
unsigned int outCount, i;
objc_property_t *properties = class_copyPropertyList(currentClass, &outCount);

if ([[propertyName lowercaseString] isEqual:[caseInsensitivePropertyName lowercaseString]])
for (i = 0; i < outCount; i++)
{
result = propertyName;
break;
objc_property_t property = properties[i];
NSString *propertyName = [NSString stringWithUTF8String:property_getName(property)];

if ([[propertyName lowercaseString] isEqual:[caseInsensitivePropertyName lowercaseString]])
{
result = propertyName;
break;
}
}

free(properties);

if (result)
return result;

currentClass = class_getSuperclass(currentClass);
}

free(properties);
return result;
return nil;
}

@end
4 changes: 2 additions & 2 deletions OCMapper/Source/ObjectMapper.m
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ - (id)init
{
if (self = [super init])
{
[self populateClassNamesInMainBundle];
[self populateClassNamesFromMainBundle];
}

return self;
Expand Down Expand Up @@ -115,7 +115,7 @@ - (id)dictionaryFromObject:(NSObject *)object

#pragma mark - Private Methods -

- (void)populateClassNamesInMainBundle
- (void)populateClassNamesFromMainBundle
{
self.classNamesInMainBundle = [NSMutableArray array];

Expand Down
12 changes: 12 additions & 0 deletions OCMapperTests/ManagedObjectMapperTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#import "CDUser.h"
#import "CDAddress.h"
#import "CDPost.h"
#import "CDSpecialUser.h"

@implementation ManagedObjectMapperTest
@synthesize mapper;
Expand Down Expand Up @@ -132,4 +133,15 @@ - (void)testNestedArrayMapping
user = nil;
}

- (void)testShouldMapPropertiesInSuperClass
{
NSMutableDictionary *userDictionary = [NSMutableDictionary dictionary];
[userDictionary setObject:@"aryan" forKey:@"firstName"];
[userDictionary setObject:@"stealth" forKey:@"power"];

CDSpecialUser *user = [self.mapper objectFromSource:userDictionary toInstanceOfClass:[CDSpecialUser class]];
STAssertTrue([user.firstName isEqual:[userDictionary objectForKey:@"firstName"]], @"date did not populate correctly");
STAssertTrue([user.power isEqual:[userDictionary objectForKey:@"power"]], @"date did not populate correctly");
}

@end
19 changes: 19 additions & 0 deletions OCMapperTests/ObjectMapperTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#import "ObjectMapper.h"
#import "User.h"
#import "Comment.h"
#import "SpecialUser.h"

@implementation ObjectMapperTests
@synthesize mapper;
Expand Down Expand Up @@ -374,4 +375,22 @@ - (void)testFlatDataToComplexObjectConversion
STAssertTrue([[userDictionary objectForKey:@"country"] isEqual:user.address.country], @"Did not populate dictionary correctly");
}

- (void)testShouldMapPropertyInSuperClass
{
NSMutableDictionary *userDictionary = [NSMutableDictionary dictionary];
[userDictionary setObject:@"Aryan" forKey:@"firstName"];
[userDictionary setObject:@"stealth" forKey:@"power"];

NSMutableDictionary *addressDictionary = [NSMutableDictionary dictionary];
[addressDictionary setObject:@"San Diego" forKey:@"city"];
[userDictionary setObject:addressDictionary forKey:@"address"];


SpecialUser *user = [self.mapper objectFromSource:userDictionary toInstanceOfClass:[SpecialUser class]];

STAssertTrue([[userDictionary objectForKey:@"firstName"] isEqual:user.firstName], @"Did not populate dictionary correctly");
STAssertTrue([[userDictionary objectForKey:@"power"] isEqual:user.power], @"Did not populate dictionary correctly");
STAssertTrue([[[userDictionary objectForKey:@"address"] objectForKey:@"city"] isEqual:user.address.city], @"Did not populate dictionary correctly");
}

@end

0 comments on commit ed5f29f

Please sign in to comment.