mirror of
https://github.com/zhigang1992/MagicalRecord.git
synced 2026-01-12 17:32:18 +08:00
Renamed old JSONHelpers to MagicalDataImport
Added implementation from other project
This commit is contained in:
@@ -37,6 +37,7 @@
|
||||
C721C87513D0C7030097AB6F /* NSPersistentStoreCoordinator+MagicalRecord.m in Sources */ = {isa = PBXBuildFile; fileRef = C721C86413D0C7030097AB6F /* NSPersistentStoreCoordinator+MagicalRecord.m */; };
|
||||
C721C87613D0C7030097AB6F /* MagicalRecordHelpers.m in Sources */ = {isa = PBXBuildFile; fileRef = C721C86713D0C7030097AB6F /* MagicalRecordHelpers.m */; };
|
||||
C721C87713D0C7030097AB6F /* MagicalRecordHelpers.m in Sources */ = {isa = PBXBuildFile; fileRef = C721C86713D0C7030097AB6F /* MagicalRecordHelpers.m */; };
|
||||
C75A4E7313D0D88D00790CEB /* NSManagedObject+MagicalDataImport.m in Sources */ = {isa = PBXBuildFile; fileRef = C75A4E7213D0D88D00790CEB /* NSManagedObject+MagicalDataImport.m */; };
|
||||
C77E5F9B13D0CA0A00298F87 /* CoreData.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C721C7E213D0C3A00097AB6F /* CoreData.framework */; };
|
||||
C77E5F9E13D0CA2100298F87 /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C77E5F9C13D0CA1800298F87 /* CoreGraphics.framework */; };
|
||||
C77E5FA113D0CA3000298F87 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C77E5F9F13D0CA2700298F87 /* UIKit.framework */; };
|
||||
@@ -49,7 +50,6 @@
|
||||
C77E5FBB13D0D2AE00298F87 /* FixtureHelpers.m in Sources */ = {isa = PBXBuildFile; fileRef = C77E5FBA13D0D2AE00298F87 /* FixtureHelpers.m */; };
|
||||
C77E5FC113D0D55300298F87 /* _SingleEntityWithNoRelationships.m in Sources */ = {isa = PBXBuildFile; fileRef = C77E5FBE13D0D55300298F87 /* _SingleEntityWithNoRelationships.m */; };
|
||||
C77E5FC213D0D55300298F87 /* SingleEntityWithNoRelationships.m in Sources */ = {isa = PBXBuildFile; fileRef = C77E5FC013D0D55300298F87 /* SingleEntityWithNoRelationships.m */; };
|
||||
C77E5FC513D0D5CD00298F87 /* NSManagedObject+MagicalDataImport.m in Sources */ = {isa = PBXBuildFile; fileRef = C77E5FC413D0D5CD00298F87 /* NSManagedObject+MagicalDataImport.m */; };
|
||||
/* End PBXBuildFile section */
|
||||
|
||||
/* Begin PBXFileReference section */
|
||||
@@ -106,6 +106,8 @@
|
||||
C721C86513D0C7030097AB6F /* CoreData+MagicalRecord.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "CoreData+MagicalRecord.h"; sourceTree = "<group>"; };
|
||||
C721C86613D0C7030097AB6F /* MagicalRecordHelpers.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MagicalRecordHelpers.h; sourceTree = "<group>"; };
|
||||
C721C86713D0C7030097AB6F /* MagicalRecordHelpers.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MagicalRecordHelpers.m; sourceTree = "<group>"; };
|
||||
C75A4E7113D0D88D00790CEB /* NSManagedObject+MagicalDataImport.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSManagedObject+MagicalDataImport.h"; sourceTree = "<group>"; };
|
||||
C75A4E7213D0D88D00790CEB /* NSManagedObject+MagicalDataImport.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSManagedObject+MagicalDataImport.m"; sourceTree = "<group>"; };
|
||||
C77E5F9C13D0CA1800298F87 /* CoreGraphics.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreGraphics.framework; path = Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS5.0.sdk/System/Library/Frameworks/CoreGraphics.framework; sourceTree = DEVELOPER_DIR; };
|
||||
C77E5F9F13D0CA2700298F87 /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS5.0.sdk/System/Library/Frameworks/UIKit.framework; sourceTree = DEVELOPER_DIR; };
|
||||
C77E5FA213D0CAC700298F87 /* GHUnitIOSTestMain.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = GHUnitIOSTestMain.m; path = "../../../../../../saul/Desktop/iOS Test Frameworks/GHUnitIOSTestMain.m"; sourceTree = "<group>"; };
|
||||
@@ -122,8 +124,6 @@
|
||||
C77E5FBE13D0D55300298F87 /* _SingleEntityWithNoRelationships.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = _SingleEntityWithNoRelationships.m; sourceTree = "<group>"; };
|
||||
C77E5FBF13D0D55300298F87 /* SingleEntityWithNoRelationships.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SingleEntityWithNoRelationships.h; sourceTree = "<group>"; };
|
||||
C77E5FC013D0D55300298F87 /* SingleEntityWithNoRelationships.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SingleEntityWithNoRelationships.m; sourceTree = "<group>"; };
|
||||
C77E5FC313D0D5CD00298F87 /* NSManagedObject+MagicalDataImport.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSManagedObject+MagicalDataImport.h"; sourceTree = "<group>"; };
|
||||
C77E5FC413D0D5CD00298F87 /* NSManagedObject+MagicalDataImport.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSManagedObject+MagicalDataImport.m"; sourceTree = "<group>"; };
|
||||
/* End PBXFileReference section */
|
||||
|
||||
/* Begin PBXFrameworksBuildPhase section */
|
||||
@@ -334,6 +334,8 @@
|
||||
C721C85813D0C7030097AB6F /* Categories */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
C75A4E7113D0D88D00790CEB /* NSManagedObject+MagicalDataImport.h */,
|
||||
C75A4E7213D0D88D00790CEB /* NSManagedObject+MagicalDataImport.m */,
|
||||
C721C85B13D0C7030097AB6F /* NSManagedObject+MagicalRecord.h */,
|
||||
C721C85C13D0C7030097AB6F /* NSManagedObject+MagicalRecord.m */,
|
||||
C721C85D13D0C7030097AB6F /* NSManagedObjectContext+MagicalRecord.h */,
|
||||
@@ -344,8 +346,6 @@
|
||||
C721C86213D0C7030097AB6F /* NSPersistentStore+MagicalRecord.m */,
|
||||
C721C86313D0C7030097AB6F /* NSPersistentStoreCoordinator+MagicalRecord.h */,
|
||||
C721C86413D0C7030097AB6F /* NSPersistentStoreCoordinator+MagicalRecord.m */,
|
||||
C77E5FC313D0D5CD00298F87 /* NSManagedObject+MagicalDataImport.h */,
|
||||
C77E5FC413D0D5CD00298F87 /* NSManagedObject+MagicalDataImport.m */,
|
||||
);
|
||||
path = Categories;
|
||||
sourceTree = "<group>";
|
||||
@@ -524,7 +524,7 @@
|
||||
C77E5FBB13D0D2AE00298F87 /* FixtureHelpers.m in Sources */,
|
||||
C77E5FC113D0D55300298F87 /* _SingleEntityWithNoRelationships.m in Sources */,
|
||||
C77E5FC213D0D55300298F87 /* SingleEntityWithNoRelationships.m in Sources */,
|
||||
C77E5FC513D0D5CD00298F87 /* NSManagedObject+MagicalDataImport.m in Sources */,
|
||||
C75A4E7313D0D88D00790CEB /* NSManagedObject+MagicalDataImport.m in Sources */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,15 +1,16 @@
|
||||
//
|
||||
// NSManagedObject+MagicalDataImport.h
|
||||
// Magical Record
|
||||
// NSManagedObject+JSONHelpers.h
|
||||
// Gathering
|
||||
//
|
||||
// Created by Saul Mora on 7/15/11.
|
||||
// Created by Saul Mora on 6/28/11.
|
||||
// Copyright 2011 Magical Panda Software LLC. All rights reserved.
|
||||
//
|
||||
|
||||
#import <CoreData/CoreData.h>
|
||||
|
||||
@interface NSManagedObject (NSManagedObject_MagicalDataImport)
|
||||
@interface NSManagedObject (NSManagedObject_JSONHelpers)
|
||||
|
||||
- (void) setValuesForKeysWithJSONDictionary:(NSDictionary *)jsonData;
|
||||
+ (NSManagedObject *) mr_importFromDictionary:(NSDictionary *)data;
|
||||
|
||||
@end
|
||||
|
||||
@@ -1,14 +1,151 @@
|
||||
//
|
||||
// NSManagedObject+MagicalDataImport.m
|
||||
// Magical Record
|
||||
// NSManagedObject+JSONHelpers.m
|
||||
// Gathering
|
||||
//
|
||||
// Created by Saul Mora on 7/15/11.
|
||||
// Created by Saul Mora on 6/28/11.
|
||||
// Copyright 2011 Magical Panda Software LLC. All rights reserved.
|
||||
//
|
||||
|
||||
#import "NSManagedObject+MagicalDataImport.h"
|
||||
|
||||
@implementation NSManagedObject (NSManagedObject_MagicalDataImport)
|
||||
static NSString * const kNSManagedObjectDefaultDateFormatString = @"YYYY-MM-dd'T'HH:mm:ss'Z'";
|
||||
static NSString * const kNSManagedObjectAttributeJSONKeyMapKey = @"jsonKeyName";
|
||||
static NSString * const kNSManagedObjectAttributeJSONValueClassNameKey = @"attributeValueClassName";
|
||||
|
||||
static NSString * const kNSManagedObjectRelationshipJSONMapKey = @"jsonKeyName";
|
||||
static NSString * const kNSManagedObjectRelationshipJSONPrimaryKey = @"primaryRelationshipKey";
|
||||
static NSString * const kNSManagedObjectRelationshipJSONTypeKey = @"type";
|
||||
|
||||
|
||||
@implementation NSString (JSONParsingHelpers)
|
||||
|
||||
- (NSDate *) mr_dateFromJSONString;
|
||||
{
|
||||
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
|
||||
[formatter setDateFormat:kNSManagedObjectDefaultDateFormatString];
|
||||
|
||||
return [formatter dateFromString:self];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@implementation NSManagedObject (NSManagedObject_JSONHelpers)
|
||||
|
||||
- (id) attributeValueFromJSONDicationary:(NSDictionary *)jsonData forAttribute:(NSAttributeDescription *)attributeInfo
|
||||
{
|
||||
NSString *attributeName = [attributeInfo name];
|
||||
NSString *lookupKey = [[attributeInfo userInfo] valueForKey:kNSManagedObjectAttributeJSONKeyMapKey] ?: attributeName;
|
||||
id value = [jsonData valueForKey:lookupKey];
|
||||
|
||||
if (value == nil || [value isEqual:[NSNull null]])
|
||||
{
|
||||
return nil;
|
||||
}
|
||||
|
||||
NSAttributeType attributeType = [attributeInfo attributeType];
|
||||
if (attributeType == NSDateAttributeType)
|
||||
{
|
||||
value = [value mr_dateFromJSONString];
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
- (void) setAttributes:(NSDictionary *)attributes forKeysWithJSONDictionary:(NSDictionary *)jsonData
|
||||
{
|
||||
for (NSString *attributeName in attributes)
|
||||
{
|
||||
NSAttributeDescription *attributeInfo = [attributes valueForKey:attributeName];
|
||||
id value = [self attributeValueFromJSONDicationary:jsonData forAttribute:attributeInfo];
|
||||
[self setValue:value forKey:attributeName];
|
||||
}
|
||||
}
|
||||
|
||||
- (NSManagedObject *) createInstanceForEntity:(NSEntityDescription *)entityDescription withJSONDictionary:(id)jsonData
|
||||
{
|
||||
NSManagedObject *relatedObject = [[NSManagedObject alloc] initWithEntity:entityDescription insertIntoManagedObjectContext:[self managedObjectContext]];
|
||||
|
||||
[relatedObject setValuesForKeysWithJSONDictionary:jsonData];
|
||||
|
||||
return relatedObject;
|
||||
}
|
||||
|
||||
- (NSManagedObject *)findObjectForRelationship:(NSRelationshipDescription *)relationshipInfo withJSONData:(id)singleRelatedObjectData
|
||||
{
|
||||
NSEntityDescription *originalDestinationEntity = [relationshipInfo destinationEntity];
|
||||
NSDictionary *subentities = [originalDestinationEntity subentitiesByName];
|
||||
NSEntityDescription *destinationEntity = [subentities count] /* && ![entityDescription isAbstract]*/ ?
|
||||
[subentities valueForKey:[singleRelatedObjectData valueForKey:kNSManagedObjectRelationshipJSONTypeKey]] :
|
||||
originalDestinationEntity;
|
||||
|
||||
if (destinationEntity == nil)
|
||||
{
|
||||
NSLog(@"Unable to find entity for type '%@'", [singleRelatedObjectData valueForKey:kNSManagedObjectRelationshipJSONTypeKey]);
|
||||
return nil;
|
||||
}
|
||||
|
||||
Class managedObjectClass = NSClassFromString([destinationEntity managedObjectClassName]);
|
||||
NSAssert([managedObjectClass isSubclassOfClass:[NSManagedObject class]], @"Entity is not a managed object! Whoa!");
|
||||
|
||||
// NSString *lookupKey = [[destinationEntity userInfo] valueForKey:kNSManagedObjectAttributeJSONKeyMapKey] ?: [destinationEntity name];
|
||||
|
||||
id existingObject = nil; //[managedObjectClass findFirstByAttribute:@"" withValue:@"" inContext:[self managedObjectContext]];
|
||||
|
||||
return existingObject ?: [self createInstanceForEntity:destinationEntity withJSONDictionary:singleRelatedObjectData];
|
||||
}
|
||||
|
||||
- (void) addObject:(NSManagedObject *)relatedObject forRelationship:(NSRelationshipDescription *)relationshipInfo
|
||||
{
|
||||
//add related object to set
|
||||
NSString *addRelationMessageFormat = [relationshipInfo isToMany] ? @"add%@Object:" : @"set%@:";
|
||||
NSString *addRelatedObjectToSetMessage = [NSString stringWithFormat:addRelationMessageFormat, [[relationshipInfo name] capitalizedString]];
|
||||
|
||||
[self performSelector:NSSelectorFromString(addRelatedObjectToSetMessage) withObject:relatedObject];
|
||||
}
|
||||
|
||||
- (void) setRelationships:(NSDictionary *)relationships forKeysWithJSONDictionary:(NSDictionary *)jsonData
|
||||
{
|
||||
for (NSString *relationshipName in relationships)
|
||||
{
|
||||
NSRelationshipDescription *relationshipInfo = [relationships valueForKey:relationshipName];
|
||||
|
||||
NSString *lookupKey = [[relationshipInfo userInfo] valueForKey:kNSManagedObjectRelationshipJSONMapKey] ?: relationshipName;
|
||||
|
||||
id relatedObjectData = [jsonData valueForKey:lookupKey];
|
||||
|
||||
if (relatedObjectData == nil || [relatedObjectData isEqual:[NSNull null]])
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if ([relationshipInfo isToMany])
|
||||
{
|
||||
for (id singleRelatedObjectData in relatedObjectData)
|
||||
{
|
||||
NSManagedObject *relatedObject = [self findObjectForRelationship:relationshipInfo
|
||||
withJSONData:singleRelatedObjectData];
|
||||
|
||||
[self addObject:relatedObject forRelationship:relationshipInfo];
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
NSManagedObject *relatedObject = [self findObjectForRelationship:relationshipInfo
|
||||
withJSONData:relatedObjectData];
|
||||
|
||||
[self addObject:relatedObject forRelationship:relationshipInfo];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
- (void) setValuesForKeysWithJSONDictionary:(NSDictionary *)jsonData
|
||||
{
|
||||
NSDictionary *attributes = [[self entity] attributesByName];
|
||||
[self setAttributes:attributes forKeysWithJSONDictionary:jsonData];
|
||||
|
||||
NSDictionary *relationships = [[self entity] relationshipsByName];
|
||||
[self setRelationships:relationships forKeysWithJSONDictionary:jsonData];
|
||||
}
|
||||
|
||||
+ (NSManagedObject *) mr_importFromDictionary:(NSDictionary *)data;
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user