Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = '2.1'
s.version = '2.2'
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 => '2.1'}
s.source = {:git => 'https://github.com/aryaxt/OCMapper.git', :tag => '2.2'}
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
1 change: 1 addition & 0 deletions OCMapper.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,7 @@
15B554A2171B7B3B0058E159 /* Project object */ = {
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0720;
LastTestingUpgradeCheck = 0610;
LastUpgradeCheck = 0500;
ORGANIZATIONNAME = "Aryan Ghassemi";
Expand Down
5 changes: 4 additions & 1 deletion OCMapper/Source/ObjectMapper.m
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ - (id)processDictionaryFromObject:(NSObject *)object
}

NSMutableDictionary *props = [NSMutableDictionary dictionary];
NSRegularExpression *camelCaseRegEx = [NSRegularExpression regularExpressionWithPattern:@"(?<=[a-z])([A-Z])|([A-Z])(?=[a-z])" options:0 error:nil];

Class currentClass = [object class];

Expand All @@ -164,7 +165,9 @@ - (id)processDictionaryFromObject:(NSObject *)object
id propertyValue = [object valueForKey:(NSString *)originalPropertyName];

ObjectMappingInfo *mapingInfo = [self.mappingProvider mappingInfoForClass:[object class] andPropertyKey:originalPropertyName];
NSString *propertyName = (mapingInfo) ? mapingInfo.dictionaryKey : originalPropertyName;

NSString *underscoreString = [[camelCaseRegEx stringByReplacingMatchesInString:originalPropertyName options:0 range:NSMakeRange(0, originalPropertyName.length) withTemplate:@"_$1$2"] lowercaseString];
NSString *propertyName = (mapingInfo) ? mapingInfo.dictionaryKey : underscoreString;

if (mapingInfo.transformer) {
propertyValue = mapingInfo.transformer(propertyValue, object);
Expand Down
2 changes: 1 addition & 1 deletion OCMapperTests/ManagedObjectMapperTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ - (void)testShouldPopulateDictionaryWithPropertyInSuperClass
user.firstName = @"Aryan";

NSDictionary *dictionary = [self.mapper dictionaryFromObject:user];
XCTAssertTrue([user.firstName isEqual:[dictionary objectForKey:@"firstName"]], @"Did Not populate dictionary properly");
XCTAssertTrue([user.firstName isEqual:[dictionary objectForKey:@"first_name"]], @"Did Not populate dictionary properly");
XCTAssertTrue([user.power isEqual:[dictionary objectForKey:@"power"]], @"Did Not populate dictionary properly");
}

Expand Down
5 changes: 1 addition & 4 deletions OCMapperTests/ObjectMapperSwiftTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ class ObjectMapperSwiftTests : XCTestCase {
"age": 28,
"dateOfBirth": date,
"location": ["name": "SF"],
"billing": ["name": "SD"],
"purchases": [
["summary": "bla 1", "price": 123.4],
["summary": "bla 2", "price": 55],
Expand All @@ -57,9 +56,6 @@ class ObjectMapperSwiftTests : XCTestCase {
XCTAssertNotNil(customer.location, "FAIL")
XCTAssertTrue(customer.location!.name == "SF", "FAIL")

XCTAssertNotNil(customer.billing, "FAIL")
XCTAssertTrue(customer.billing!.name == "SD", "FAIL")

let purchases = customer.valueForKey("purchases") as! NSArray

// Can't access calues directly in unit test target, swift throws exception because it doesn't know which target the models belong to, main target or unit test target. This won't be an issue outside of test environment
Expand All @@ -79,6 +75,7 @@ class ObjectMapperSwiftTests : XCTestCase {
mappingProvider.mapFromDictionaryKey("address", toPropertyKey: "location", withObjectType: Location.self, forClass: Customer.self)
mappingProvider.mapFromDictionaryKey("billing-address", toPropertyKey: "billing", withObjectType: Location.self, forClass: Customer.self)
mappingProvider.mapFromDictionaryKey("orders", toPropertyKey: "purchases", withObjectType: Purchase.self, forClass: Customer.self)
mappingProvider.mapFromDictionaryKey("status", toPropertyKey: "status", withObjectType: ActivationStatus.self, forClass: Customer.self)

let date = NSDate().dateByAddingTimeInterval(-5555)

Expand Down
14 changes: 7 additions & 7 deletions OCMapperTests/ObjectMapperTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,8 @@ - (void)testDictionaryFromFlatObject

NSDictionary *dictionary = [self.mapper dictionaryFromObject:user];

XCTAssertTrue([[dictionary objectForKey:@"firstName"] isEqual:user.firstName], @"Did not populate dictionary correctly");
XCTAssertTrue([[dictionary objectForKey:@"lastName"] isEqual:user.lastName], @"Did not populate dictionary correctly");
XCTAssertTrue([[dictionary objectForKey:@"first_name"] isEqual:user.firstName], @"Did not populate dictionary correctly");
XCTAssertTrue([[dictionary objectForKey:@"last_name"] isEqual:user.lastName], @"Did not populate dictionary correctly");
XCTAssertTrue([[dictionary objectForKey:@"age"] isEqual:user.age], @"Did not populate dictionary correctly");
}

Expand Down Expand Up @@ -425,7 +425,7 @@ - (void)testShouldPopulateDictionaryWithPropertyInSuperClass
user.firstName = @"Aryan";

NSDictionary *dictionary = [self.mapper dictionaryFromObject:user];
XCTAssertTrue([user.firstName isEqual:[dictionary objectForKey:@"firstName"]], @"Did Not populate dictionary properly");
XCTAssertTrue([user.firstName isEqual:[dictionary objectForKey:@"first_name"]], @"Did Not populate dictionary properly");
XCTAssertTrue([user.power isEqual:[dictionary objectForKey:@"power"]], @"Did Not populate dictionary properly");
}

Expand Down Expand Up @@ -484,7 +484,7 @@ - (void)testShouldMapArrayOfStringFromObjectToDictionary
user.randomKeywords = @[@"keyword1", @2].mutableCopy;

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

XCTAssertTrue(array.count == 2);
XCTAssertTrue([array[0] isEqualToString:@"keyword1"]);
Expand All @@ -498,11 +498,11 @@ - (void)testShouldNotMapExcludedKeys {
user.age = @28;
user.dateOfBirth = [NSDate date];

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

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