Skip to content

Commit

Permalink
See #7, #10. Started implementing serialization and saving of records…
Browse files Browse the repository at this point in the history
… with adapter and serializer.
  • Loading branch information
Glavin001 committed Jul 31, 2014
1 parent a807ee1 commit c12ebad
Show file tree
Hide file tree
Showing 10 changed files with 256 additions and 280 deletions.
6 changes: 3 additions & 3 deletions Common/SLAdapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ typedef NS_ENUM(NSUInteger, SLHTTPMethodType)
@property (strong, nonatomic, setter=setEmail:) NSString *userEmail;
@property (strong, nonatomic, setter=setPassword:) NSString *userPassword;
@property (strong, nonatomic, setter=setOrganization:) NSString *userOrganization;

@property (strong, nonatomic) SLSerializer *serializer;

/**
SHA-1 encoding of a plain text string.
Expand Down Expand Up @@ -100,7 +100,7 @@ typedef NS_ENUM(NSUInteger, SLHTTPMethodType)
/**
Serializes the record and send it to the server.
*/
- (PMKPromise *) createRecord:(Class)modelClass withId:(SLNid)nid withStore:(SLStore *)store;
- (PMKPromise *) createRecord:(SLModel *)record withStore:(SLStore *)store;
/*
Serializes the record update and send it to the server.
*/
Expand Down Expand Up @@ -128,7 +128,7 @@ typedef NS_ENUM(NSUInteger, SLHTTPMethodType)
/**
Proxies to the serializer's serialize method.
*/
- (NSDictionary *) serialize:(NSDictionary *)options;
- (NSDictionary *) serialize:(SLModel *)record withOptions:(NSDictionary *)options;

/**
Builds a URL for a given type and optional ID.
Expand Down
20 changes: 16 additions & 4 deletions Common/SLAdapter.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
#import "SLObjectIdTransform.h"

@interface SLAdapter () {

}

@property (strong, nonatomic) AFHTTPRequestOperationManager* httpManager;

@end

@implementation SLAdapter
@synthesize userEmail = _userEmail, userPassword = _userPassword, userOrganization = _userOrganization, host, httpManager;
@synthesize userEmail = _userEmail, userPassword = _userPassword, userOrganization = _userOrganization, host, httpManager, serializer;

- (instancetype) init
{
Expand All @@ -35,6 +35,7 @@ - (instancetype) init
_userPassword = nil;
_userOrganization = nil;
host = nil;
serializer = [[SLSerializer alloc] init];
//httpManager = [AFHTTPRequestOperationManager manager];
httpManager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:nil];
httpManager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
Expand Down Expand Up @@ -290,7 +291,6 @@ - (PMKPromise *) authenticateWithUserEmail:(NSString *)theEmail
NSDictionary *response = (NSDictionary *)responseObject;

//
SLSerializer *serializer = [[SLSerializer alloc] init];
NSDictionary *serialized = [serializer extractSingle:[SLUser class] withPayload:response withStore:[SLStore sharedStore]];

fulfiller(PMKManifold(serialized, operation));
Expand All @@ -312,6 +312,13 @@ - (PMKPromise *) authenticateWithUserEmail:(NSString *)theEmail
}];
}

- (PMKPromise *) createRecord:(SLModel *)record withStore:(SLStore *)store;
{
NSDictionary *options = @{};
NSDictionary *data = [self serialize:record withOptions:options];
NSString *path = [NSString stringWithFormat:@"%@/", [[record class] type]];
return [self performRequestWithMethod:SLHTTPMethodPOST withPath:path withParameters:data];
}

- (PMKPromise *) findAll:(Class)modelClass withStore:(SLStore *)store
{
Expand All @@ -324,7 +331,7 @@ - (PMKPromise *) findMany:(Class)modelClass withIds:(NSArray *)ids withStore:(SL
NSLog(@"ids: %@", ids);
// Map IDs to ObjectId
NSMutableArray *nids = [NSMutableArray array];
for (NSDictionary *i in ids)
for (NSString *i in ids)
{
NSDictionary *nid = [SLObjectIdTransform serialize:i];
[nids addObject:nid];
Expand Down Expand Up @@ -352,5 +359,10 @@ - (PMKPromise *) findQuery:(Class)modelClass withQuery:(NSDictionary *)query wit
}];
}

- (NSDictionary *) serialize:(SLModel *)record withOptions:(NSDictionary *)options
{
return [serializer serialize:record withOptions:options];
}


@end
31 changes: 16 additions & 15 deletions Common/SLModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@
*/
- (instancetype) initInContext:(NSManagedObjectContext *)context;

+ (instancetype) initInContext:(NSManagedObjectContext *)context;

/**
Returns an object initialized with the specific `SLNid` nid.
Expand Down Expand Up @@ -138,20 +140,14 @@


/**
Return the {type} of this node.
Return the {type} of this model.
*/
- (NSString *) type;

/**
Returns the current value of the `SLValue` of the node's data with the key `attr`.
*/
- (id) get:(NSString *)attr;

/**
Update a single attribute. Updating a node sets it's internal boolean,
{isSaved}, false.
*/
- (void) update:(NSString *)attr value:(id)value;
- (PMKPromise *) get:(NSString *)attr;


/**
Expand Down Expand Up @@ -245,14 +241,19 @@
- (NSDictionary *) serialize:(NSDictionary *)options;

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

/**
Get all model Attributes by name.
Push some data for a given type into the store.
This method expects normalized data:
The ID is a key named id (an ID is mandatory)
The names of attributes are the ones you used in your model's DS.attrs.
Your relationships must be:
represented as IDs or Arrays of IDs
represented as model instances
represented as URLs, under the links key
*/
+ (NSDictionary *) relationshipsByName;
- (SLModel *) pushWithData:(NSDictionary *)datum;


@end
Loading

0 comments on commit c12ebad

Please sign in to comment.