Skip to content

Commit

Permalink
See #7. Worked on Serialization and Deserialization for Adapter and S…
Browse files Browse the repository at this point in the history
…erializer and added more Unit Tests.
  • Loading branch information
Glavin001 committed Jul 21, 2014
1 parent 8b6aa32 commit 463dd27
Show file tree
Hide file tree
Showing 8 changed files with 211 additions and 94 deletions.
15 changes: 14 additions & 1 deletion Common/Models/SLAsset.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,20 @@ @implementation SLAsset

+ (NSString *) type
{
return @"asset";
return @"assets";
}


+ (NSString *) keyForAttribute:(NSString *)attribute
{
attribute = [super keyForAttribute:attribute];

if ([attribute isEqualToString:@"desc"])
{
return @"description";
}
return attribute;
}


@end
15 changes: 8 additions & 7 deletions Common/SLAdapter.m
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ - (instancetype) init
// Handle Response
httpManager.responseSerializer = [AFJSONResponseSerializer serializer];
httpManager.responseSerializer.acceptableContentTypes = nil;

}
return self;
}
Expand Down Expand Up @@ -135,10 +134,11 @@ - (PMKPromise *) performRequestWithMethod:(SLHTTPMethodType)theMethod
NSLog(@"thePath: %@", thePath);
NSString *absPath = [NSString stringWithFormat:@"/%@/%@", @"api/v1", thePath];
NSLog(@"absPath: %@", absPath);
NSURL *fullPath = [[NSURL alloc] initWithScheme:@"http" host:self.host path:absPath];
// NSURL *fullPath = [NSURL URLWithString:[NSString stringWithFormat:@"%@", thePath] relativeToURL:self.host];
NSLog(@"fullPath: %@", fullPath);
NSString *fullPathStr = [fullPath absoluteString];
// NSURL *fullPathURL = [[NSURL alloc] initWithScheme:@"http" host:self.host path:absPath];
// NSURL *fullPath = [NSURL URLWithString:[NSString stringWithFormat:@"%@", thePath] relativeToURL:self.host];
// NSLog(@"fullPath: %@", fullPathURL);
// NSString *fullPathStr = [fullPathURL absoluteString];
NSString *fullPathStr = [NSString stringWithFormat:@"%@://%@%@", @"http", self.host, absPath];
NSLog(@"Full path: %@", fullPathStr);

// Prepare headers used for authentication
Expand Down Expand Up @@ -238,7 +238,7 @@ - (PMKPromise *) performRequestWithMethod:(SLHTTPMethodType)theMethod
break;
case SLHTTPMethodDELETE:
{
[self.httpManager DELETE:[fullPath absoluteString] parameters:theParams success:^(AFHTTPRequestOperation *operation, id responseObject) {
[self.httpManager DELETE:fullPathStr parameters:theParams success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success, JSON: %@", responseObject);
fulfiller(PMKManifold(responseObject, operation));
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
Expand Down Expand Up @@ -294,7 +294,8 @@ - (PMKPromise *) authenticateWithUserEmail:(NSString *)theEmail

- (PMKPromise *) findAll:(Class)modelClass withStore:(SLStore *)store
{
return [self performRequestWithMethod:SLHTTPMethodGET withPath:[modelClass type] withParameters:nil];
NSString *path = [NSString stringWithFormat:@"%@/", [modelClass type]];
return [self performRequestWithMethod:SLHTTPMethodGET withPath:path withParameters:nil];
}

//- (NSString *) buildURL:(Class)modelClass
Expand Down
9 changes: 7 additions & 2 deletions Common/SLModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,14 @@
Edit when subclassing, if neccessary.
*/
- (NSString *) keyForAttribute:(NSString *)attribute;
+ (NSString *) keyForAttribute:(NSString *)attribute;

/**
Relationship to Key mappings for the Model.
Edit when subclassing, if neccessary.
*/
- (NSString *) keyForRelationship:(NSString *)relationship;
+ (NSString *) keyForRelationship:(NSString *)relationship;

/**
Returns an NSArray of pending Nodes.
Expand Down Expand Up @@ -237,5 +237,10 @@
*/
- (NSDictionary *) serialize:(NSDictionary *)options;

/**
Get all model Attributes by name.
*/
+ (NSDictionary *) attributesByName;


@end
105 changes: 63 additions & 42 deletions Common/SLModel.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ - (instancetype) init {
- (instancetype) initInContext:(NSManagedObjectContext *)context
{
NSLog(@"init %@", [self class]);
NSLog(@"inManagedObjectContext: %@", context.persistentStoreCoordinator.managedObjectModel.entities);
// NSLog(@"inManagedObjectContext: %@", context.persistentStoreCoordinator.managedObjectModel.entities);


//self = [super init];
Expand Down Expand Up @@ -74,7 +74,7 @@ + (instancetype) initWithId:(SLNid)nid inContext:(NSManagedObjectContext *)conte
}
}

- (NSString *) keyForAttribute:(NSString *)attribute
+ (NSString *) keyForAttribute:(NSString *)attribute
{
if ([attribute isEqualToString:@"dateCreated"])
{
Expand All @@ -87,7 +87,7 @@ - (NSString *) keyForAttribute:(NSString *)attribute
return attribute;
}

- (NSString *) keyForRelationship:(NSString *)relationship {
+ (NSString *) keyForRelationship:(NSString *)relationship {
return relationship;
}

Expand All @@ -98,6 +98,29 @@ + (NSArray *) pending
}


/*
- (NSString *) description
{
//return [NSString stringWithFormat:@"<%@>", [self class]];
return [NSString stringWithFormat:@"<%@ %p: %@>", [self class], self,
[NSDictionary dictionaryWithObjectsAndKeys:
NSNullIfNil([self nid]), @"id",
NSNullIfNil([self type]), @"type",
NSNullIfNil([self.data description]), @"data",
NSNullIfNil([self.rels description]), @"relationships",
nil
] ];
//return [NSString stringWithFormat:@"<%@: { type: \"%@\", data: %@, relationships: %@ } >", [self class], [self type], [self.data description], [self.rels description]];
}
*/

- (NSString *) type
{
return [[self class] type];
}

+ (NSString *) type
{
// return NSStringFromClass([instance class]);
Expand All @@ -106,6 +129,7 @@ + (NSString *) type
userInfo:nil];
}


+ (PMKPromise *) readById:(SLNid)nid
{
NSDictionary *filters = @{
Expand Down Expand Up @@ -310,29 +334,6 @@ __block void (^ completionCallback)() = ^{
}];
}

/*
- (NSString *) description
{
//return [NSString stringWithFormat:@"<%@>", [self class]];
return [NSString stringWithFormat:@"<%@ %p: %@>", [self class], self,
[NSDictionary dictionaryWithObjectsAndKeys:
NSNullIfNil([self nid]), @"id",
NSNullIfNil([self type]), @"type",
NSNullIfNil([self.data description]), @"data",
NSNullIfNil([self.rels description]), @"relationships",
nil
] ];
//return [NSString stringWithFormat:@"<%@: { type: \"%@\", data: %@, relationships: %@ } >", [self class], [self type], [self.data description], [self.rels description]];
}
*/

- (NSString *) type
{
return [[self class] type];
}

- (PMKPromise *) pushWithAPIManager:(SLAdapter *)manager
{
return [PMKPromise new:^(PMKPromiseFulfiller fulfiller, PMKPromiseRejecter rejecter) {
Expand All @@ -357,15 +358,35 @@ - (PMKPromise *) pushWithAPIManager:(SLAdapter *)manager
}];
}

+ (NSEntityDescription *) entity
{
NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
return [NSEntityDescription entityForName:NSStringFromClass([self class]) inManagedObjectContext:context];
}

+ (NSDictionary *) attributesByName
{
NSEntityDescription *entityDescription = self.entity;
NSDictionary *attributes = [entityDescription attributesByName];
return attributes;
}

- (NSDictionary *) attributesByName
{
NSEntityDescription *entityDescription = self.entity;
NSDictionary *attributes = [entityDescription attributesByName];
return attributes;
}

- (NSDictionary *) serializeData {
NSMutableDictionary *theData = [NSMutableDictionary dictionary];
NSEntityDescription *entityDescription = [self entity];
NSEntityDescription *entityDescription = self.entity;
NSDictionary *attributes = [entityDescription attributesByName];
//NSLog(@"%@", attributes);
for (NSAttributeDescription *attribute in attributes) {
for (NSString *attributeKey in attributes) {
//NSLog(@"attribute: %@ = %@", attribute, [self valueForKey:(NSString *)attribute]);
if ( ![attribute isEqual:@"syncState"] ) {
[theData setValue:[self valueForKey:(NSString *)attribute] forKey:[self keyForAttribute:(NSString *)attribute]];
if ( ![attributeKey isEqual:@"syncState"] ) {
[theData setValue:[self valueForKey:(NSString *)attributeKey] forKey:[[self class] keyForAttribute:(NSString *)attributeKey]];
}
}
return [NSDictionary dictionaryWithDictionary:theData];
Expand All @@ -377,16 +398,16 @@ - (PMKPromise *) remove
return [[self class] deleteWithNode:self];
}

- (void) didChangeValueForKey:(NSString *)key {
NSLog(@"didChangeValueForKey %@", key);
if (
( ![key isEqual: @"syncState"] ) &&
[[self syncState] isEqual: @(SLSyncStateSynced)]
) {
[self setSyncState:@(SLSyncStatePendingUpdate)];
}
}

//- (void) didChangeValueForKey:(NSString *)key {
// NSLog(@"didChangeValueForKey %@", key);
// if (
// ( ![key isEqual: @"syncState"] ) &&
// [[self syncState] isEqual: @(SLSyncStateSynced)]
// ) {
// [self setSyncState:@(SLSyncStatePendingUpdate)];
// }
//}
//

+ (instancetype) createRecord:(NSDictionary *)properties
{
Expand Down Expand Up @@ -435,8 +456,8 @@ - (NSDictionary *) serialize:(NSDictionary *)options

- (instancetype) setupData:(NSDictionary *)data
{
NSLog(@"inManagedObjectContext: %@", self.managedObjectContext.persistentStoreCoordinator.managedObjectModel.entities);
NSEntityDescription *modelEntity = [NSEntityDescription entityForName:NSStringFromClass([self class]) inManagedObjectContext:self.managedObjectContext];
// NSLog(@"inManagedObjectContext: %@", self.managedObjectContext.persistentStoreCoordinator.managedObjectModel.entities);
NSEntityDescription *modelEntity = self.entity;

// Attributes
NSDictionary *attributes = [modelEntity attributesByName];
Expand Down
33 changes: 30 additions & 3 deletions Common/SLSerializer.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

#import "SLSerializer.h"
#import "SLModel.h"

// Transforms
#import "SLObjectIdTransform.h"
Expand Down Expand Up @@ -35,6 +36,11 @@ - (NSDictionary *)extractSingle:(Class)modelClass withPayload:(NSDictionary *)pa
return results;
}

- (NSArray *) extractArray:(Class)modelClass withPayload:(NSDictionary *)payload withStore:(SLStore *)store
{
return @[];
}

- (NSDictionary *)normalize:(Class)modelClass withPayload:(NSDictionary *)payload
{
NSDictionary *results = [self normalizeIdWithPayload:payload];
Expand All @@ -60,9 +66,30 @@ - (NSDictionary *)normalizeIdWithPayload:(NSDictionary *)payload

- (NSDictionary *)normalizeAttributes:(Class)modelClass withPayload:(NSDictionary *)payload
{
// TODO: Iterate thru attributes
// TODO: Handle different types of Attributes with Transforms
return payload;
NSDictionary *results = [NSDictionary dictionaryWithDictionary:payload];
NSDictionary *attributes = [modelClass attributesByName];
for (NSString *attributeKey in attributes)
{
NSAttributeDescription *attribute = attributes[attributeKey];

// TODO: Handle different types of Attributes with Transforms
NSAttributeType type = [attribute attributeType];

// TODO: Handle renaming keys for attributes
NSLog(@"attribute: %@", attribute);
NSLog(@"attributeKey: %@", attributeKey);
NSString *payloadKey = [modelClass keyForAttribute:attributeKey];
NSLog(@"payloadKey: %@", payloadKey);
if (![attributeKey isEqualToString:payloadKey])
{
// Attribute's Key is different
// Rename it.
// FIXME:
//[results setValue:val forKey:<#(NSString *)#>]
}

}
return results;
}

- (NSDictionary *)normalizeRelationships:(Class)modelClass withPayload:(NSDictionary *)payload
Expand Down
5 changes: 5 additions & 0 deletions Common/SLStore.m
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,9 @@ - (SLModel *) record:(Class)modelClass forId:(SLNid)nid
return record;
}

- (NSArray *) pushMany:(Class)modelClass withData:(NSArray *)data
{
return @[];
}

@end
Loading

0 comments on commit 463dd27

Please sign in to comment.