Skip to content

Commit

Permalink
Merge branch 'master' of github.com:keithpitt/DKCoreData
Browse files Browse the repository at this point in the history
  • Loading branch information
keithpitt committed Oct 23, 2011
2 parents 45b9b9c + 44365f6 commit 74ea8fd
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 16 deletions.
1 change: 1 addition & 0 deletions Classes/DKManagedObject.h
Expand Up @@ -26,6 +26,7 @@
+ (NSDictionary*)lookupSynchronizationObject:(NSDictionary *)data;
+ (void)synchronizeFromJSON:(NSArray *)data;

+ (void)destroyAllUsingQuery:(DKCoreDataQuery *)query;
+ (void)destroyAll;

+ (id)find:(NSNumber *)identifier;
Expand Down
25 changes: 17 additions & 8 deletions Classes/DKManagedObject.m
Expand Up @@ -114,14 +114,21 @@ + (void)synchronizeFromJSON:(NSArray*)data {

}

+ (void)destroyAll {
// Delete all records for this Object that match the provided DKCoreDataQuery
+ (void)destroyAllUsingQuery:(DKCoreDataQuery *)query {

NSArray *records = [[self query] results];
NSArray *records = [query results];

for (int i = 0, l = [records count]; i < l; i++)
[[records objectAtIndex:i] destroy];

[[DKCoreData shared] saveManagedObjectContext];
}

// Deletes all records for this Object
+ (void)destroyAll {

[self destroyAllUsingQuery:[self query]];

}

Expand Down Expand Up @@ -322,7 +329,7 @@ - (void)updateAttributes:(NSDictionary*)data {
NSEntityDescription * destination = [relationshipDescription destinationEntity];

// Create the association class
Class assocaitionClass = NSClassFromString([destination managedObjectClassName]);
Class associationClass = NSClassFromString([destination managedObjectClassName]);

if ([relationshipDescription isToMany]) {

Expand All @@ -332,14 +339,16 @@ - (void)updateAttributes:(NSDictionary*)data {

for (NSDictionary * jsonData in associations) {

// The data for the association.
NSDictionary * associationData = [jsonData objectForKey:[[assocaitionClass entityName] lowercaseString]];
// The data for the association
// Converts the associationClass into a string identifier - EG: LineItem becomes a key of "line_item"
// Therefore, hash data should look like {"line_items" => {"line_item" => {"attribute" => "value"}}}
NSDictionary * associationData = [jsonData objectForKey:[[associationClass entityName] underscore]];

// Grab the ID and convert it an NSNumber
NSNumber * identifier = [NSNumber numberWithString:[associationData objectForKey:@"id"]];

// Find or build the assocaition
DKManagedObject * record = [assocaitionClass findOrBuildBy:[assocaitionClass primaryKey] value:identifier managedObjectContext:[self managedObjectContext]];
DKManagedObject * record = [associationClass findOrBuildBy:[associationClass primaryKey] value:identifier managedObjectContext:[self managedObjectContext]];
[record updateAttributes:associationData];

[records addObject:record];
Expand All @@ -351,14 +360,14 @@ - (void)updateAttributes:(NSDictionary*)data {

} else {

// The data for the association.
// The data for the association
NSDictionary * associationData = [data objectForKey:jsonKey];

// Grab the ID and convert it an NSNumber
NSNumber * identifier = [NSNumber numberWithString:[associationData objectForKey:@"id"]];

// Find or build the assocaition
DKManagedObject * record = [assocaitionClass findOrBuildBy:[assocaitionClass primaryKey] value:identifier managedObjectContext:[self managedObjectContext]];
DKManagedObject * record = [associationClass findOrBuildBy:[associationClass primaryKey] value:identifier managedObjectContext:[self managedObjectContext]];
[record updateAttributes:associationData];

// Set the association back to the base record
Expand Down
20 changes: 12 additions & 8 deletions DKCoreData.xcodeproj/project.pbxproj
Expand Up @@ -55,10 +55,11 @@
53FFD00014011C1000E6B764 /* DKFile.m in Sources */ = {isa = PBXBuildFile; fileRef = 53FFCFFF14011C1000E6B764 /* DKFile.m */; };
53FFD00214011C1F00E6B764 /* libicucore.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 53FFD00114011C1F00E6B764 /* libicucore.dylib */; };
53FFD00514011C8200E6B764 /* Specs.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = 53FFD00314011C8200E6B764 /* Specs.xcdatamodeld */; };
78AB61AB141764C500C449EE /* ProfileWithUserAndComments.json in Resources */ = {isa = PBXBuildFile; fileRef = 78AB61AA141764C500C449EE /* ProfileWithUserAndComments.json */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
5304AF7D140A80FB00DF687E /* UsersWithNameKeithPitty.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = UsersWithNameKeithPitty.json; sourceTree = "<group>"; };
5304AF7D140A80FB00DF687E /* UsersWithNameKeithPitty.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = UsersWithNameKeithPitty.json; sourceTree = "<group>"; };
53247F84140B485800C33061 /* DKPredicate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = DKPredicate.h; path = DKPredicateBuilder/Classes/DKPredicate.h; sourceTree = "<group>"; };
53247F85140B485800C33061 /* DKPredicate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = DKPredicate.m; path = DKPredicateBuilder/Classes/DKPredicate.m; sourceTree = "<group>"; };
53247F86140B485800C33061 /* DKPredicateBuilder.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = DKPredicateBuilder.h; path = DKPredicateBuilder/Classes/DKPredicateBuilder.h; sourceTree = "<group>"; };
Expand All @@ -71,13 +72,13 @@
5348724B14011996003522DA /* DKCoreDataQuery.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DKCoreDataQuery.m; sourceTree = "<group>"; };
5348724C14011996003522DA /* DKManagedObject.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DKManagedObject.h; sourceTree = "<group>"; };
5348724D14011996003522DA /* DKManagedObject.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DKManagedObject.m; sourceTree = "<group>"; };
53487254140119AB003522DA /* ImportableUsers.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = ImportableUsers.json; sourceTree = "<group>"; };
53487255140119AB003522DA /* InvalidCafesFormat.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = InvalidCafesFormat.json; sourceTree = "<group>"; };
53487256140119AB003522DA /* NonExistantModel.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = NonExistantModel.json; sourceTree = "<group>"; };
53487257140119AB003522DA /* OddNumberOfUsers.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = OddNumberOfUsers.json; sourceTree = "<group>"; };
53487258140119AB003522DA /* UsersWithComments.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = UsersWithComments.json; sourceTree = "<group>"; };
53487259140119AB003522DA /* UsersWithNameKeithPitt.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = UsersWithNameKeithPitt.json; sourceTree = "<group>"; };
5348725A140119AB003522DA /* UsersWithNoIdentifiers.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = UsersWithNoIdentifiers.json; sourceTree = "<group>"; };
53487254140119AB003522DA /* ImportableUsers.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = ImportableUsers.json; sourceTree = "<group>"; };
53487255140119AB003522DA /* InvalidCafesFormat.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = InvalidCafesFormat.json; sourceTree = "<group>"; };
53487256140119AB003522DA /* NonExistantModel.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = NonExistantModel.json; sourceTree = "<group>"; };
53487257140119AB003522DA /* OddNumberOfUsers.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = OddNumberOfUsers.json; sourceTree = "<group>"; };
53487258140119AB003522DA /* UsersWithComments.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = UsersWithComments.json; sourceTree = "<group>"; };
53487259140119AB003522DA /* UsersWithNameKeithPitt.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = UsersWithNameKeithPitt.json; sourceTree = "<group>"; };
5348725A140119AB003522DA /* UsersWithNoIdentifiers.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = UsersWithNoIdentifiers.json; sourceTree = "<group>"; };
5348725B140119AB003522DA /* DKCoreDataQuerySpec.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DKCoreDataQuerySpec.m; sourceTree = "<group>"; };
5348725C140119AB003522DA /* DKDataImporterSpec.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DKDataImporterSpec.m; sourceTree = "<group>"; };
5348725D140119AB003522DA /* DKManagedObjectSpec.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DKManagedObjectSpec.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -155,6 +156,7 @@
53FFCFFF14011C1000E6B764 /* DKFile.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = DKFile.m; path = DKFile/Classes/DKFile.m; sourceTree = "<group>"; };
53FFD00114011C1F00E6B764 /* libicucore.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libicucore.dylib; path = Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.3.sdk/usr/lib/libicucore.dylib; sourceTree = DEVELOPER_DIR; };
53FFD00414011C8200E6B764 /* Specs.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = Specs.xcdatamodel; sourceTree = "<group>"; };
78AB61AA141764C500C449EE /* ProfileWithUserAndComments.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = ProfileWithUserAndComments.json; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -212,6 +214,7 @@
53487259140119AB003522DA /* UsersWithNameKeithPitt.json */,
5304AF7D140A80FB00DF687E /* UsersWithNameKeithPitty.json */,
5348725A140119AB003522DA /* UsersWithNoIdentifiers.json */,
78AB61AA141764C500C449EE /* ProfileWithUserAndComments.json */,
);
path = Data;
sourceTree = "<group>";
Expand Down Expand Up @@ -528,6 +531,7 @@
53487270140119AB003522DA /* UsersWithNameKeithPitt.json in Resources */,
53487271140119AB003522DA /* UsersWithNoIdentifiers.json in Resources */,
5304AF7E140A80FB00DF687E /* UsersWithNameKeithPitty.json in Resources */,
78AB61AB141764C500C449EE /* ProfileWithUserAndComments.json in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
28 changes: 28 additions & 0 deletions Specs/DKDataImporterSpec.m
Expand Up @@ -15,6 +15,7 @@

#import "FGSpecImportableUser.h"

#import "FGSpecProfile.h"
#import "FGSpecUser.h"
#import "FGSpecComment.h"

Expand Down Expand Up @@ -198,6 +199,33 @@
} background:NO];

});

it(@"should import nested has many relationships", ^{

[FGSpecProfile destroyAll];
[FGSpecUser destroyAll];
[FGSpecComment destroyAll];

NSArray * profileWithUserAndComments = [DKFile jsonFromBundle:nil pathForResource:@"ProfileWithUserAndComments"];

[DKCoreDataImporter import:^(DKCoreDataImporter * importer) {

expect([importer import:profileWithUserAndComments]).toBeTruthy();

} completion: ^{

int profileCount = [[FGSpecProfile query] count];
expect(profileCount).toEqual(1);

int usersCount = [[FGSpecUser query] count];
expect(usersCount).toEqual(1);

int commentsCount = [[FGSpecComment query] count];
expect(commentsCount).toEqual(2);

} background:NO];

});

});

Expand Down
25 changes: 25 additions & 0 deletions Specs/Data/ProfileWithUserAndComments.json
@@ -0,0 +1,25 @@
[{
"profile": {
"user": {
"comments": [
{
"comment": {
"body": "Comment 1",
"id": 37821
}
},
{
"comment": {
"body": "Comment 2",
"id": 1599397
}
}],
"id": 1,
"last_name": "Pitt",
"email": "me@keithpitt.com",
"first_name": "Keith"
},
"biography": "Some biography!",
"id": 1
}
}]

0 comments on commit 74ea8fd

Please sign in to comment.