Skip to content
This repository has been archived by the owner on Mar 23, 2021. It is now read-only.

Make NSCoding implementation always encode properties to NSData, and de... #40

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 15 additions & 7 deletions nsrails/Source/NSRRemoteObject.m
Expand Up @@ -40,7 +40,7 @@ a copy of this software and associated documentation files (the

@interface NSRRemoteObject (private)

- (NSDictionary *) remoteDictionaryRepresentationWrapped:(BOOL)wrapped fromNesting:(BOOL)nesting;
- (NSDictionary *) dictionaryRepresentationWrapped:(BOOL)wrapped fromNesting:(BOOL)nesting remoteOnly:(BOOL)remote;
+ (NSArray *) arrayOfInstancesFromRemoteJSON:(id)json;

- (BOOL) propertyIsTimestamp:(NSString *)property;
Expand Down Expand Up @@ -209,14 +209,18 @@ - (id) encodeValueForProperty:(NSString *)property remoteKey:(NSString **)remote

for (id element in val)
{
id encodedObj = [element remoteDictionaryRepresentationWrapped:NO fromNesting:YES];
id encodedObj = [element dictionaryRepresentationWrapped:NO
fromNesting:YES
remoteOnly:YES];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Weird... the colons are matching up in xCode, for me. Have you ever heard this happen before?

[new addObject:encodedObj];
}

return new;
}

return [val remoteDictionaryRepresentationWrapped:NO fromNesting:YES];
return [val dictionaryRepresentationWrapped:NO
fromNesting:YES
remoteOnly:YES];
}

if ([val isKindOfClass:[NSDate class]])
Expand Down Expand Up @@ -383,16 +387,16 @@ - (void) setPropertiesUsingRemoteDictionary:(NSDictionary *)dict

- (NSDictionary *) remoteDictionaryRepresentationWrapped:(BOOL)wrapped
{
return [self remoteDictionaryRepresentationWrapped:wrapped fromNesting:NO];
return [self dictionaryRepresentationWrapped:wrapped fromNesting:NO remoteOnly:YES];
}

- (NSDictionary *) remoteDictionaryRepresentationWrapped:(BOOL)wrapped fromNesting:(BOOL)nesting
- (NSDictionary *) dictionaryRepresentationWrapped:(BOOL)wrapped fromNesting:(BOOL)nesting remoteOnly:(BOOL)remote
{
NSMutableDictionary *dict = [NSMutableDictionary dictionary];

for (NSString *objcProperty in [self remoteProperties])
{
if (![self shouldSendProperty:objcProperty whenNested:nesting])
if (remote && ![self shouldSendProperty:objcProperty whenNested:nesting])
continue;

NSString *remoteKey = objcProperty;
Expand Down Expand Up @@ -607,6 +611,7 @@ - (id) initWithCoder:(NSCoder *)aDecoder
{
self.remoteID = [aDecoder decodeObjectForKey:@"remoteID"];
remoteAttributes = [aDecoder decodeObjectForKey:@"remoteAttributes"];
[self setPropertiesUsingRemoteDictionary:remoteAttributes];
self.remoteDestroyOnNesting = [aDecoder decodeBoolForKey:@"remoteDestroyOnNesting"];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My tabbing isn't like this here, either! Very confused..

}
return self;
Expand All @@ -615,7 +620,10 @@ - (id) initWithCoder:(NSCoder *)aDecoder
- (void) encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:self.remoteID forKey:@"remoteID"];
[aCoder encodeObject:remoteAttributes forKey:@"remoteAttributes"];
[aCoder encodeObject:[self dictionaryRepresentationWrapped:NO
fromNesting:NO
remoteOnly:NO]
forKey:@"remoteAttributes"];
[aCoder encodeBool:remoteDestroyOnNesting forKey:@"remoteDestroyOnNesting"];
}

Expand Down
30 changes: 30 additions & 0 deletions nsrails/Tests/RemoteObject.m
Expand Up @@ -598,5 +598,35 @@ - (void) test_update_nested_object
STAssertEqualObjects(b.name, @"tweety", nil);
}

- (void) test_nscoding
{
Post *pBefore = [[Post alloc] init];

pBefore.remoteID = @5;
pBefore.author = @"Arthur McWriterston";
pBefore.content = @"My name is ridiculous.";
pBefore.updatedAt = [NSDate date];
pBefore.remoteDestroyOnNesting = YES;

NSData* pData = [NSKeyedArchiver archivedDataWithRootObject:pBefore];
Post* pAfter = [NSKeyedUnarchiver unarchiveObjectWithData:pData];


STAssertEqualObjects(pBefore.remoteID, pAfter.remoteID,
@"The post's remoteID should match before and after being archived.");
STAssertEqualObjects(pBefore.content, pAfter.content,
@"The post's remoteID should match before and after being archived.");
STAssertEqualObjects(pBefore.author, pAfter.author,
@"The post's author should match before and after being archived.");

// During the encoding/decoding process, we seem to lose a few milli/nanoseconds.
// Get around caring about these by doing an NSTimeInterval, which goes by seconds.
NSInteger tiBetween = [pBefore.updatedAt timeIntervalSinceDate:pAfter.updatedAt];
STAssertTrue(tiBetween == 0,
@"The post's updatedAt should match before and after being archived.");
STAssertTrue(pAfter.remoteDestroyOnNesting, @"remoteDestroyOnNesting should match before and after being archived.");
}



@end