Updates to the mapper

This commit is contained in:
Blake Watters
2009-08-08 17:27:16 -04:00
parent b9e74ee2f5
commit 425810a409
4 changed files with 39 additions and 6 deletions

View File

@@ -94,7 +94,12 @@
/**
* Fetch a resource via an HTTP GET and invoke a callback with the model for the resulting payload
*/
- (OTRestRequest*)getAndMap:(NSString*)resourcePath delegate:(id)delegate callback:(SEL)callback;
- (OTRestRequest*)getModel:(NSString*)resourcePath delegate:(id)delegate callback:(SEL)callback;
/**
* Fetch a resource via an HTTP GET and invoke a callback with the model for the resulting payload
*/
- (OTRestRequest*)getModels:(NSString*)resourcePath delegate:(id)delegate callback:(SEL)callback;
/**
* Register a model mapping from a domain model class to an XML element name

View File

@@ -18,7 +18,7 @@ static OTRestClient* sharedClient = nil;
@implementation OTRestClient
@synthesize baseURL = _baseURL, username = _username, password = _password, HTTPHeaders = _HTTPHeaders;
@synthesize baseURL = _baseURL, username = _username, password = _password, HTTPHeaders = _HTTPHeaders, mapper = _mapper;
+ (OTRestClient*)client {
return sharedClient;
@@ -116,9 +116,18 @@ static OTRestClient* sharedClient = nil;
loader.delegate = delegate;
loader.callback = callback;
return [self get:resourcePath delegate:loader callback:loader.mapperCallback];
return [self get:resourcePath delegate:loader callback:loader.memberCallback];
}
// Add a collection oriented accessor
/**
* Load a collection of models from a restful resource and invoke the callback
*/
- (OTRestRequest*)getModels:(NSString*)resourcePath delegate:(id)delegate callback:(SEL)callback {
OTRestModelLoader* loader = [[OTRestModelLoader alloc] initWithMapper:self.mapper];
loader.delegate = delegate;
loader.callback = callback;
return [self get:resourcePath delegate:loader callback:loader.collectionCallback];
}
@end

View File

@@ -33,7 +33,12 @@
/**
* The method to invoke to trigger model mappings. Used as the callback for a restful model mapping request
*/
@property (nonatomic, readonly) SEL mapperCallback;
@property (nonatomic, readonly) SEL memberCallback;
/**
* The method to invoke to trigger model mappings. Used as the callback for a restful model mapping request
*/
@property (nonatomic, readonly) SEL collectionCallback;
/**

View File

@@ -20,13 +20,27 @@
return self;
}
- (SEL)mapperCallback {
- (SEL)memberCallback {
return @selector(loadModelFromXML:);
}
- (SEL)collectionCallback {
return @selector(loadModelsFromXML:);
}
- (void)loadModelFromXML:(Element*)XML {
id model = [_mapper buildModelFromXML:XML];
[_delegate performSelector:self.callback withObject:model];
}
- (void)loadModelsFromXML:(Element*)XML {
NSMutableArray* models = [[[NSMutableArray alloc] init] autorelease];
NSArray* elements = [XML syblingElements];
for (Element* element in elements) {
id model = [_mapper buildModelFromXML:element];
[models addObject:model];
}
[_delegate performSelector:self.callback withObject:models];
}
@end