Skip to content

Commit

Permalink
Merge pull request #18 from hyperoslo/feature/reserved-words
Browse files Browse the repository at this point in the history
Add support for reserved attributes
  • Loading branch information
zenangst committed Nov 5, 2014
2 parents 2393776 + ffac818 commit 4359f85
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
<entity name="User" syncable="YES">
<attribute name="age" optional="YES" attributeType="Integer 16" defaultValueString="0" syncable="YES"/>
<attribute name="birthDate" optional="YES" attributeType="Date" syncable="YES"/>
<attribute name="contractID" optional="YES" attributeType="Integer 32" defaultValueString="0" syncable="YES"/>
<attribute name="driverIdentifier" optional="YES" attributeType="String" syncable="YES"/>
<attribute name="firstName" optional="YES" attributeType="String" syncable="YES"/>
<attribute name="lastName" optional="YES" attributeType="String" syncable="YES"/>
<attribute name="userDescription" optional="YES" attributeType="String" syncable="YES"/>
<attribute name="userID" optional="YES" attributeType="Integer 32" defaultValueString="0" syncable="YES"/>
<attribute name="userType" optional="YES" attributeType="String" syncable="YES"/>
</entity>
<elements>
<element name="User" positionX="-63" positionY="-18" width="128" height="135"/>
<element name="User" positionX="-63" positionY="-18" width="128" height="180"/>
</elements>
</model>
2 changes: 1 addition & 1 deletion NSManagedObject-HYPPropertyMapper.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "NSManagedObject-HYPPropertyMapper"
s.version = "1.7"
s.version = "2.0"
s.summary = "Mapping your Core Data objects with your JSON providing backend has never been this easy"
s.description = <<-DESC
* Mapping your Core Data objects with your JSON providing backend has never been this easy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,15 @@ - (void)setUp
self.testUser = [NSEntityDescription insertNewObjectForEntityForName:@"User"
inManagedObjectContext:self.managedObjectContext];

[self.testUser setValue:@25 forKey:@"age"];
[self.testUser setValue:[NSDate date] forKey:@"birthDate"];
[self.testUser setValue:@235 forKey:@"contractID"];
[self.testUser setValue:@"ABC8283" forKey:@"driverIdentifier"];
[self.testUser setValue:@"John" forKey:@"firstName"];
[self.testUser setValue:@"Hyperseed" forKey:@"lastName"];
[self.testUser setValue:@"John Description" forKey:@"userDescription"];
[self.testUser setValue:@111 forKey:@"userID"];
[self.testUser setValue:@"Manager" forKey:@"userType"];
}

- (void)tearDown
Expand Down Expand Up @@ -150,33 +157,56 @@ - (void)testLocalString

#pragma mark - Property Mapper

- (void)testDictionaryKeys
#pragma mark hyp_dictionary

- (void)testDictionaryKeysNotNil
{
NSDictionary *dictionary = [self.testUser hyp_dictionary];

XCTAssertNotNil(dictionary[@"age"]);

XCTAssertNotNil(dictionary[@"birth_date"]);

XCTAssertNotNil(dictionary[@"contract_id"]);

XCTAssertNotNil(dictionary[@"driver_identifier"]);

XCTAssertNotNil(dictionary[@"first_name"]);

XCTAssertNotNil(dictionary[@"last_name"]);

XCTAssertNotNil(dictionary[@"description"]);

XCTAssertNotNil(dictionary[@"id"]);

XCTAssertNotNil(dictionary[@"type"]);
}

- (void)testDictionaryValues
- (void)testDictionaryValuesKindOfClass
{
NSDictionary *dictionary = [self.testUser hyp_dictionary];

__block BOOL valid = YES;
[dictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
NSString *localString = [key localString];
id value = [self.testUser valueForKey:localString];
XCTAssertTrue([dictionary[@"age"] isKindOfClass:[NSNumber class]]);

XCTAssertTrue([dictionary[@"birth_date"] isKindOfClass:[NSDate class]]);

XCTAssertTrue([dictionary[@"contract_id"] isKindOfClass:[NSNumber class]]);

XCTAssertTrue([dictionary[@"driver_identifier"] isKindOfClass:[NSString class]]);

XCTAssertTrue([dictionary[@"first_name"] isKindOfClass:[NSString class]]);

XCTAssertTrue([dictionary[@"last_name"] isKindOfClass:[NSString class]]);

XCTAssertTrue([dictionary[@"description"] isKindOfClass:[NSString class]]);

if (![value isEqual:obj]) {
*stop = YES;
valid = NO;
}
}];
XCTAssertTrue([dictionary[@"id"] isKindOfClass:[NSNumber class]]);

XCTAssert(valid, @"Dictionary values match object values");
XCTAssertTrue([dictionary[@"type"] isKindOfClass:[NSString class]]);
}

#pragma mark - hyp_fillWithDictionary

- (void)testFillManagedObjectWithDictionary
{
NSDictionary *values = @{
Expand Down Expand Up @@ -304,4 +334,22 @@ - (void)testAcronyms
XCTAssertEqualObjects([self.testUser valueForKey:@"userID"], @100);
}


- (void)testReservedWords
{
NSDictionary *values = @{
@"id": @100,
@"description": @"This is the description?",
@"type": @"user type"
};

[self.testUser hyp_fillWithDictionary:values];

XCTAssertEqualObjects([self.testUser valueForKey:@"userID"], @100);

XCTAssertEqualObjects([self.testUser valueForKey:@"userDescription"], @"This is the description?");

XCTAssertEqualObjects([self.testUser valueForKey:@"userType"], @"user type");
}

@end
49 changes: 44 additions & 5 deletions Source/NSManagedObject+HYPPropertyMapper.m
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,16 @@ @implementation NSManagedObject (HYPPropertyMapper)

- (void)hyp_fillWithDictionary:(NSDictionary *)dictionary
{
for (NSString *remoteKey in dictionary) {
for (__strong NSString *remoteKey in dictionary) {

id value = [dictionary objectForKey:remoteKey];
id propertyDescription = [self propertyDescriptionForKey:remoteKey];

BOOL isReservedKey = ([[NSManagedObject reservedAttributes] containsObject:remoteKey]);
if (isReservedKey) {
remoteKey = [self prefixedAttribute:remoteKey];
}

id propertyDescription = [self propertyDescriptionForKey:remoteKey];
if (!propertyDescription) continue;

NSString *localKey = [propertyDescription name];
Expand Down Expand Up @@ -218,18 +223,52 @@ - (NSDictionary *)hyp_dictionary
for (id propertyDescription in [self.entity properties]) {

if ([propertyDescription isKindOfClass:[NSAttributeDescription class]]) {
NSAttributeDescription *attributeDescription = (NSAttributeDescription *)propertyDescription;
NSString *key = [[propertyDescription name] remoteString];

NSAttributeDescription *attributeDescription = (NSAttributeDescription *)propertyDescription;
id value = [self valueForKey:[attributeDescription name]];

if (!value || [value isKindOfClass:[NSNull class]]) continue;

NSMutableString *key = [[[propertyDescription name] remoteString] mutableCopy];
BOOL isReservedKey = ([[self reservedKeys] containsObject:key]);
if (isReservedKey) {
[key replaceOccurrencesOfString:[self remotePrefix]
withString:@""
options:NSCaseInsensitiveSearch
range:NSMakeRange(0, key.length)];
}

mutableDictionary[key] = value;
}
}

return [mutableDictionary copy];
}

- (NSString *)remotePrefix
{
return [NSString stringWithFormat:@"%@_", [self.entity.name lowercaseString]];
}

- (NSString *)prefixedAttribute:(NSString *)attribute
{
return [NSString stringWithFormat:@"%@%@", [self remotePrefix], attribute];
}

- (NSArray *)reservedKeys
{
NSMutableArray *keys = [NSMutableArray array];
NSArray *reservedAttributes = [NSManagedObject reservedAttributes];

for (NSString *attribute in reservedAttributes) {
[keys addObject:[self prefixedAttribute:attribute]];
}

return keys;
}

+ (NSArray *)reservedAttributes
{
return @[@"id", @"type", @"description", @"signed"];
}

@end

0 comments on commit 4359f85

Please sign in to comment.