mirror of
https://github.com/zhigang1992/RestKit.git
synced 2026-05-27 00:08:52 +08:00
Merge branch '0.9' into xml-parser
Conflicts: Examples/RKTwitter/Classes/RKTwitterViewController.m README.md RestKit.xcodeproj/project.pbxproj
This commit is contained in:
@@ -8,8 +8,8 @@
|
||||
|
||||
#import <objc/message.h>
|
||||
|
||||
// TODO: Factor out Core Data...
|
||||
#import "../CoreData/CoreData.h"
|
||||
#import "RKObjectManager.h"
|
||||
|
||||
#import "RKObjectMapper.h"
|
||||
#import "NSDictionary+RKAdditions.h"
|
||||
@@ -135,12 +135,12 @@ static const NSString* kRKModelMapperXMLMappingFormatParserKey = @"RKXMLMappingF
|
||||
}
|
||||
|
||||
// Primary entry point for RKObjectLoader
|
||||
- (id)mapFromString:(NSString*)string toClass:(Class)class keyPath:(NSString*)keyPath {
|
||||
- (id)mapFromString:(NSString*)string toClass:(Class<RKObjectMappable>)class keyPath:(NSString*)keyPath {
|
||||
id object = [self parseString:string];
|
||||
if (keyPath) {
|
||||
object = [object valueForKeyPath:keyPath];
|
||||
}
|
||||
|
||||
|
||||
if ([object isKindOfClass:[NSDictionary class]]) {
|
||||
if (class) {
|
||||
return [self mapObjectFromDictionary:(NSDictionary*)object toClass:class];
|
||||
@@ -167,7 +167,7 @@ static const NSString* kRKModelMapperXMLMappingFormatParserKey = @"RKXMLMappingF
|
||||
return [self mapFromString:string toClass:nil keyPath:nil];
|
||||
}
|
||||
|
||||
- (void)mapObject:(id)model fromString:(NSString*)string {
|
||||
- (void)mapObject:(NSObject<RKObjectMappable>*)model fromString:(NSString*)string {
|
||||
id object = [self parseString:string];
|
||||
if ([object isKindOfClass:[NSDictionary class]]) {
|
||||
[self mapObject:model fromDictionary:object];
|
||||
@@ -183,8 +183,7 @@ static const NSString* kRKModelMapperXMLMappingFormatParserKey = @"RKXMLMappingF
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Mapping from objects
|
||||
|
||||
// TODO: Should accept RKObjectMappable instead of id...
|
||||
- (void)mapObject:(id)model fromDictionary:(NSDictionary*)dictionary {
|
||||
- (void)mapObject:(NSObject<RKObjectMappable>*)model fromDictionary:(NSDictionary*)dictionary {
|
||||
Class class = [model class];
|
||||
|
||||
NSArray* elementNames = [_elementToClassMappings allKeysForObject:class];
|
||||
@@ -208,18 +207,30 @@ static const NSString* kRKModelMapperXMLMappingFormatParserKey = @"RKXMLMappingF
|
||||
}
|
||||
}
|
||||
|
||||
- (id)mapObjectFromDictionary:(NSDictionary*)dictionary toClass:(Class)class {
|
||||
- (NSObject<RKObjectMappable>*)mapObjectFromDictionary:(NSDictionary*)dictionary toClass:(Class)class {
|
||||
return [self createOrUpdateInstanceOfModelClass:class fromElements:dictionary];
|
||||
}
|
||||
|
||||
- (id)mapObjectFromDictionary:(NSDictionary*)dictionary {
|
||||
// TODO: Makes assumptions about the structure of the JSON...
|
||||
// Lookup an object type using the element to class mappings and map its attributes
|
||||
// Expects an object of the form {"registered_type": {"attribute1": "value", "attribute2": "value"}
|
||||
- (NSObject<RKObjectMappable>*)mapObjectFromDictionary:(NSDictionary*)dictionary {
|
||||
// TODO: Makes assumptions about the structure of the JSON...
|
||||
NSString* elementName = [[dictionary allKeys] objectAtIndex:0];
|
||||
Class class = [_elementToClassMappings objectForKey:elementName];
|
||||
NSDictionary* elements = [dictionary objectForKey:elementName];
|
||||
id elements = [dictionary objectForKey:elementName];
|
||||
|
||||
id model = [self findOrCreateInstanceOfModelClass:class fromElements:elements];
|
||||
[self updateModel:model fromElements:elements];
|
||||
// Support case where elements is an Array of objects
|
||||
id model = nil;
|
||||
if ([elements isKindOfClass:[NSDictionary class]]) {
|
||||
model = [self findOrCreateInstanceOfModelClass:class fromElements:elements];
|
||||
[self updateModel:model fromElements:elements];
|
||||
} else if ([elements isKindOfClass:[NSArray class]]) {
|
||||
model = [self mapObjectsFromArrayOfDictionaries:elements toClass:class];
|
||||
} else {
|
||||
// TODO: Do we want to blow up or log???
|
||||
NSLog(@"Unable to determine how to map object: %@", elements);
|
||||
}
|
||||
|
||||
return model;
|
||||
}
|
||||
|
||||
@@ -240,7 +251,7 @@ static const NSString* kRKModelMapperXMLMappingFormatParserKey = @"RKXMLMappingF
|
||||
return (NSArray*)objects;
|
||||
}
|
||||
|
||||
- (NSArray*)mapObjectsFromArrayOfDictionaries:(NSArray*)array toClass:(Class)class {
|
||||
- (NSArray*)mapObjectsFromArrayOfDictionaries:(NSArray*)array toClass:(Class<RKObjectMappable>)class {
|
||||
NSMutableArray* objects = [NSMutableArray array];
|
||||
for (NSDictionary* dictionary in array) {
|
||||
if (![dictionary isKindOfClass:[NSNull class]]) {
|
||||
@@ -267,9 +278,10 @@ static const NSString* kRKModelMapperXMLMappingFormatParserKey = @"RKXMLMappingF
|
||||
// Persistent Instance Finders
|
||||
|
||||
// TODO: This version does not update properties. Should probably be realigned.
|
||||
- (id)findOrCreateInstanceOfModelClass:(Class)class fromElements:(NSDictionary*)elements {
|
||||
- (NSObject<RKObjectMappable>*)findOrCreateInstanceOfModelClass:(Class)class fromElements:(NSDictionary*)elements {
|
||||
id object = nil;
|
||||
if ([class isSubclassOfClass:[RKManagedObject class]]) {
|
||||
Class managedObjectClass = NSClassFromString(@"RKManagedObject");
|
||||
if (managedObjectClass && [class isSubclassOfClass:managedObjectClass]) {
|
||||
NSString* primaryKeyElement = [class performSelector:@selector(primaryKeyElement)];
|
||||
id primaryKeyValue = [elements objectForKey:primaryKeyElement];
|
||||
object = [[[RKObjectManager sharedManager] objectStore] findOrCreateInstanceOfManagedObject:class
|
||||
@@ -288,7 +300,7 @@ static const NSString* kRKModelMapperXMLMappingFormatParserKey = @"RKXMLMappingF
|
||||
return object;
|
||||
}
|
||||
|
||||
- (id)createOrUpdateInstanceOfModelClass:(Class)class fromElements:(NSDictionary*)elements {
|
||||
- (NSObject<RKObjectMappable>*)createOrUpdateInstanceOfModelClass:(Class<RKObjectMappable>)class fromElements:(NSDictionary*)elements {
|
||||
id model = [self findOrCreateInstanceOfModelClass:class fromElements:elements];
|
||||
[self updateModel:model fromElements:elements];
|
||||
return model;
|
||||
@@ -297,7 +309,7 @@ static const NSString* kRKModelMapperXMLMappingFormatParserKey = @"RKXMLMappingF
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Property & Relationship Manipulation
|
||||
|
||||
- (void)updateModel:(id)model ifNewPropertyValue:(id)propertyValue forPropertyNamed:(NSString*)propertyName {
|
||||
- (void)updateModel:(NSObject<RKObjectMappable>*)model ifNewPropertyValue:(id)propertyValue forPropertyNamed:(NSString*)propertyName {
|
||||
id currentValue = [model valueForKey:propertyName];
|
||||
if (nil == currentValue && nil == propertyValue) {
|
||||
// Don't set the property, both are nil
|
||||
@@ -335,7 +347,7 @@ static const NSString* kRKModelMapperXMLMappingFormatParserKey = @"RKXMLMappingF
|
||||
}
|
||||
}
|
||||
|
||||
- (void)setPropertiesOfModel:(id)model fromElements:(NSDictionary*)elements {
|
||||
- (void)setPropertiesOfModel:(NSObject<RKObjectMappable>*)model fromElements:(NSDictionary*)elements {
|
||||
NSDictionary* elementToPropertyMappings = [self elementToPropertyMappingsForModel:model];
|
||||
for (NSString* elementKeyPath in elementToPropertyMappings) {
|
||||
id elementValue = nil;
|
||||
@@ -388,7 +400,7 @@ static const NSString* kRKModelMapperXMLMappingFormatParserKey = @"RKXMLMappingF
|
||||
}
|
||||
}
|
||||
|
||||
- (void)setRelationshipsOfModel:(id)object fromElements:(NSDictionary*)elements {
|
||||
- (void)setRelationshipsOfModel:(NSObject<RKObjectMappable>*)object fromElements:(NSDictionary*)elements {
|
||||
NSDictionary* elementToRelationshipMappings = [[object class] elementToRelationshipMappings];
|
||||
for (NSString* elementKeyPath in elementToRelationshipMappings) {
|
||||
NSString* propertyName = [elementToRelationshipMappings objectForKey:elementKeyPath];
|
||||
@@ -436,8 +448,10 @@ static const NSString* kRKModelMapperXMLMappingFormatParserKey = @"RKXMLMappingF
|
||||
[object setValue:child forKey:propertyName];
|
||||
}
|
||||
|
||||
}
|
||||
if ([object isKindOfClass:[RKManagedObject class]]) {
|
||||
}
|
||||
|
||||
Class managedObjectClass = NSClassFromString(@"RKManagedObject");
|
||||
if (managedObjectClass && [object isKindOfClass:managedObjectClass]) {
|
||||
RKManagedObject* managedObject = (RKManagedObject*)object;
|
||||
NSDictionary* relationshipToPkPropertyMappings = [[managedObject class] relationshipToPrimaryKeyPropertyMappings];
|
||||
for (NSString* relationship in relationshipToPkPropertyMappings) {
|
||||
@@ -454,7 +468,7 @@ static const NSString* kRKModelMapperXMLMappingFormatParserKey = @"RKXMLMappingF
|
||||
NSEntityDescription* relationshipDestinationEntity = [[relationshipsByName objectForKey:relationship] destinationEntity];
|
||||
id relationshipDestinationClass = objc_getClass([[relationshipDestinationEntity managedObjectClassName] cStringUsingEncoding:NSUTF8StringEncoding]);
|
||||
RKManagedObject* relationshipValue = [[[RKObjectManager sharedManager] objectStore] findOrCreateInstanceOfManagedObject:relationshipDestinationClass
|
||||
withPrimaryKeyValue:objectPrimaryKeyValue];
|
||||
withPrimaryKeyValue:objectPrimaryKeyValue];
|
||||
if (relationshipValue) {
|
||||
[managedObject setValue:relationshipValue forKey:relationship];
|
||||
}
|
||||
@@ -463,7 +477,7 @@ static const NSString* kRKModelMapperXMLMappingFormatParserKey = @"RKXMLMappingF
|
||||
|
||||
}
|
||||
|
||||
- (void)updateModel:(id)model fromElements:(NSDictionary*)elements {
|
||||
- (void)updateModel:(NSObject<RKObjectMappable>*)model fromElements:(NSDictionary*)elements {
|
||||
[self setPropertiesOfModel:model fromElements:elements];
|
||||
[self setRelationshipsOfModel:model fromElements:elements];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user