Permalink
Browse files

Extracted MagicalRecord helper methods into separate category files

  • Loading branch information...
1 parent 666550b commit df242726d9df1ef19931076ecd1353c9c095c831 @casademora casademora committed Mar 7, 2012
Showing with 1,016 additions and 897 deletions.
  1. +27 −0 MagicalRecord/Categories/DataImport/MagicalImportFunctions.h
  2. +101 −0 MagicalRecord/Categories/DataImport/MagicalImportFunctions.m
  3. +1 −1 MagicalRecord/Categories/DataImport/NSEntityDescription+MagicalDataImport.m
  4. +1 −1 MagicalRecord/Categories/DataImport/NSObject+MagicalDataImport.m
  5. +1 −1 MagicalRecord/Categories/DataImport/NSRelationshipDescription+MagicalDataImport.m
  6. +1 −1 MagicalRecord/Categories/NSManagedObject+MagicalDataImport.h
  7. +16 −12 MagicalRecord/Categories/NSManagedObject+MagicalDataImport.m
  8. +1 −1 MagicalRecord/Categories/NSManagedObject+MagicalRecord.h
  9. +5 −12 MagicalRecord/Categories/NSManagedObject+MagicalRecord.m
  10. +1 −1 MagicalRecord/Categories/NSManagedObjectContext+MagicalRecord.h
  11. +17 −64 MagicalRecord/Categories/NSManagedObjectContext+MagicalRecord.m
  12. +1 −1 MagicalRecord/Categories/NSManagedObjectModel+MagicalRecord.h
  13. +1 −5 MagicalRecord/Categories/NSManagedObjectModel+MagicalRecord.m
  14. +1 −1 MagicalRecord/Categories/NSPersistentStore+MagicalRecord.h
  15. +1 −4 MagicalRecord/Categories/NSPersistentStore+MagicalRecord.m
  16. +1 −1 MagicalRecord/Categories/NSPersistentStoreCoordinator+MagicalRecord.h
  17. +7 −14 MagicalRecord/Categories/NSPersistentStoreCoordinator+MagicalRecord.m
  18. +30 −57 MagicalRecord/CoreData+MagicalRecord.h
  19. +3 −3 MagicalRecord/{MRCoreDataAction.h → MagicalRecord+Actions.h}
  20. +1 −7 MagicalRecord/{MRCoreDataAction.m → MagicalRecord+Actions.m}
  21. +22 −0 MagicalRecord/MagicalRecord+ErrorHandling.h
  22. +94 −0 MagicalRecord/MagicalRecord+ErrorHandling.m
  23. +24 −0 MagicalRecord/MagicalRecord+Options.h
  24. +38 −0 MagicalRecord/MagicalRecord+Options.m
  25. +21 −0 MagicalRecord/MagicalRecord+Setup.h
  26. +51 −0 MagicalRecord/MagicalRecord+Setup.m
  27. +15 −0 MagicalRecord/MagicalRecord+ShorthandSupport.h
  28. +146 −0 MagicalRecord/MagicalRecord+ShorthandSupport.m
  29. +18 −0 MagicalRecord/MagicalRecord+iCloud.h
  30. +45 −0 MagicalRecord/MagicalRecord+iCloud.m
  31. +29 −0 MagicalRecord/MagicalRecord.h
  32. +91 −0 MagicalRecord/MagicalRecord.m
  33. +0 −90 MagicalRecord/MagicalRecordHelpers.h
  34. +0 −493 MagicalRecord/MagicalRecordHelpers.m
  35. +7 −5 Project Files/Mac App Unit Tests/GHUnitTestMain.m
  36. +158 −73 Project Files/Magical Record.xcodeproj/project.pbxproj
  37. +2 −6 Project Files/Unit Tests/Fixtures/iOS/TestModel.xcdatamodeld/TestModel.xcdatamodel/contents
  38. +9 −9 Project Files/Unit Tests/ImportSingleEntityWithNoRelationshipsTests.m
  39. +4 −4 Project Files/Unit Tests/MagicalDataImportTestCase.m
  40. +1 −1 Project Files/Unit Tests/{MagicalRecordHelperTests.h → MagicalRecordTests.h}
  41. +14 −14 Project Files/Unit Tests/{MagicalRecordHelperTests.m → MagicalRecordTests.m}
  42. +3 −8 Project Files/Unit Tests/NSManagedObjectContextHelperTests.m
  43. +2 −2 Project Files/Unit Tests/NSManagedObjectHelperTests.m
  44. +4 −4 Project Files/Unit Tests/NSPersistentStoreCoordinatorHelperTests.m
  45. +0 −1 Project Files/iOS App Unit Tests/iOS App Unit Tests-Prefix.pch
@@ -0,0 +1,27 @@
+//
+// MagicalImportFunctions.h
+// Magical Record
+//
+// Created by Saul Mora on 3/7/12.
+// Copyright (c) 2012 Magical Panda Software LLC. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+
+NSDate * adjustDateForDST(NSDate *date);
+NSDate * dateFromString(NSString *value, NSString *format);
+NSString * attributeNameFromString(NSString *value);
+NSString * primaryKeyNameFromString(NSString *value);
+
+#if TARGET_OS_IPHONE
+
+UIColor * UIColorFromString(NSString *serializedColor);
+
+#else
+
+NSColor * NSColorFromString(NSString *serializedColor);
+
+#endif
+extern id (*colorFromString)(NSString *);
+
@@ -0,0 +1,101 @@
+//
+// MagicalImportFunctions.m
+// Magical Record
+//
+// Created by Saul Mora on 3/7/12.
+// Copyright (c) 2012 Magical Panda Software LLC. All rights reserved.
+//
+
+#import "MagicalImportFunctions.h"
+
+
+#pragma mark - Data import helper functions
+
+NSString * attributeNameFromString(NSString *value)
+{
+ NSString *firstCharacter = [[value substringToIndex:1] capitalizedString];
+ return [firstCharacter stringByAppendingString:[value substringFromIndex:1]];
+}
+
+NSString * primaryKeyNameFromString(NSString *value)
+{
+ NSString *firstCharacter = [[value substringToIndex:1] lowercaseString];
+ return [firstCharacter stringByAppendingFormat:@"%@ID", [value substringFromIndex:1]];
+}
+
+NSDate * adjustDateForDST(NSDate *date)
+{
+ NSTimeInterval dstOffset = [[NSTimeZone localTimeZone] daylightSavingTimeOffsetForDate:date];
+ NSDate *actualDate = [date dateByAddingTimeInterval:dstOffset];
+
+ return actualDate;
+}
+
+NSDate * dateFromString(NSString *value, NSString *format)
+{
+ NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
+ [formatter setTimeZone:[NSTimeZone localTimeZone]];
+ [formatter setLocale:[NSLocale currentLocale]];
+ [formatter setDateFormat:format];
+
+ NSDate *parsedDate = [formatter dateFromString:value];
+
+ return parsedDate;
+}
+
+NSInteger* newColorComponentsFromString(NSString *serializedColor);
+NSInteger* newColorComponentsFromString(NSString *serializedColor)
+{
+ NSScanner *colorScanner = [NSScanner scannerWithString:serializedColor];
+ NSString *colorType;
+ [colorScanner scanUpToString:@"(" intoString:&colorType];
+
+ NSInteger *componentValues = malloc(4 * sizeof(NSInteger));
+ if ([colorType hasPrefix:@"rgba"])
+ {
+ NSCharacterSet *rgbaCharacterSet = [NSCharacterSet characterSetWithCharactersInString:@"(,)"];
+
+ NSInteger *componentValue = componentValues;
+ while (![colorScanner isAtEnd])
+ {
+ [colorScanner scanCharactersFromSet:rgbaCharacterSet intoString:nil];
+ [colorScanner scanInteger:componentValue];
+ componentValue++;
+ }
+ }
+ //else if ([colorType hasPrefix:@"hsba"])
+ //else if ([colorType hasPrefix:@""])
+ return componentValues;
+}
+
+#if TARGET_OS_IPHONE
+
+UIColor * UIColorFromString(NSString *serializedColor)
+{
+ NSInteger *componentValues = newColorComponentsFromString(serializedColor);
+ UIColor *color = [UIColor colorWithRed:(componentValues[0] / 255.)
+ green:(componentValues[1] / 255.)
+ blue:(componentValues[2] / 255.)
+ alpha:componentValues[3]];
+
+ free(componentValues);
+ return color;
+}
+id (*colorFromString)(NSString *) = UIColorFromString;
+
+#else
+
+NSColor * NSColorFromString(NSString *serializedColor)
+{
+ NSInteger *componentValues = newColorComponentsFromString(serializedColor);
+ NSColor *color = [NSColor colorWithDeviceRed:(componentValues[0] / 255.)
+ green:(componentValues[1] / 255.)
+ blue:(componentValues[2] / 255.)
+ alpha:componentValues[3]];
+ free(componentValues);
+ return color;
+}
+id (*colorFromString)(NSString *) = NSColorFromString;
+
+
+#endif
@@ -28,7 +28,7 @@ - (NSManagedObject *) MR_createInstanceFromDictionary:(id)objectData inContext:(
NSManagedObject *relatedObject = [[self class] insertNewObjectForEntityForName:[self name]
inManagedObjectContext:context];
- [relatedObject MR_importValuesForKeysWithDictionary:objectData];
+ [relatedObject MR_importValuesForKeysWithObject:objectData];
return relatedObject;
}
@@ -8,7 +8,7 @@
#import "NSObject+MagicalDataImport.h"
#import "NSManagedObject+MagicalDataImport.h"
-#import "MagicalRecordHelpers.h"
+#import "MagicalRecord.h"
#import "CoreData+MagicalRecord.h"
NSUInteger const kMagicalRecordImportMaximumAttributeFailoverDepth = 10;
@@ -8,7 +8,7 @@
#import "NSRelationshipDescription+MagicalDataImport.h"
#import "NSManagedObject+MagicalDataImport.h"
-#import "MagicalRecordHelpers.h"
+#import "MagicalRecord.h"
@implementation NSRelationshipDescription (MagicalRecord_DataImport)
@@ -19,7 +19,7 @@ extern NSString * const kMagicalRecordImportRelationshipTypeKey;
@interface NSManagedObject (MagicalRecord_DataImport)
-- (void) MR_importValuesForKeysWithDictionary:(id)objectData;
+- (void) MR_importValuesForKeysWithObject:(id)objectData;
- (void) MR_updateValuesForKeysWithDictionary:(id)objectData;
+ (id) MR_importFromDictionary:(id)data;
@@ -9,7 +9,7 @@
#import "CoreData+MagicalRecord.h"
#import <objc/runtime.h>
-void swizzle(Class c, SEL orig, SEL new);
+void MR_swizzle(Class c, SEL orig, SEL new);
NSString * const kMagicalRecordImportCustomDateFormatKey = @"dateFormat";
NSString * const kMagicalRecordImportDefaultDateFormatString = @"yyyy-MM-dd'T'HH:mm:ss'Z'";
@@ -163,6 +163,9 @@ - (void) MR_setRelationships:(NSDictionary *)relationships forKeysWithDictionary
SEL shouldImportSelector = @selector(shouldImport:);
BOOL implementsShouldImport = [self respondsToSelector:shouldImportSelector];
+
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
if (![self MR_importValue:relatedObjectData forKey:relationshipName])
{
@@ -185,12 +188,13 @@ - (void) MR_setRelationships:(NSDictionary *)relationships forKeysWithDictionary
}
}
}
+#pragma clang diagnostic pop
}
}
-- (void) MR_importValuesForKeysWithDictionary:(id)objectData
+- (void) MR_importValuesForKeysWithObject:(id)objectData
{
- swizzle([objectData class], @selector(valueForUndefinedKey:), @selector(MR_valueForUndefinedKey:));
+ MR_swizzle([objectData class], @selector(valueForUndefinedKey:), @selector(MR_valueForUndefinedKey:));
if ([self respondsToSelector:@selector(willImport)])
{
[self performSelector:@selector(willImport)];
@@ -224,11 +228,11 @@ - (void) MR_importValuesForKeysWithDictionary:(id)objectData
{
relatedObject = [self MR_findObjectForRelationship:relationshipInfo withData:objectData];
}
- [relatedObject MR_importValuesForKeysWithDictionary:objectData];
+ [relatedObject MR_importValuesForKeysWithObject:objectData];
[self MR_addObject:relatedObject forRelationship:relationshipInfo];
}];
- swizzle([objectData class], @selector(valueForUndefinedKey:), @selector(MR_valueForUndefinedKey:));
+ MR_swizzle([objectData class], @selector(valueForUndefinedKey:), @selector(MR_valueForUndefinedKey:));
if ([self respondsToSelector:@selector(didImport)])
{
@@ -238,7 +242,7 @@ - (void) MR_importValuesForKeysWithDictionary:(id)objectData
- (void) MR_updateValuesForKeysWithDictionary:(id)objectData
{
- swizzle([objectData class], @selector(valueForUndefinedKey:), @selector(MR_valueForUndefinedKey:));
+ MR_swizzle([objectData class], @selector(valueForUndefinedKey:), @selector(MR_valueForUndefinedKey:));
if ([self respondsToSelector:@selector(willImport)])
{
[self performSelector:@selector(willImport)];
@@ -266,7 +270,7 @@ - (void) MR_updateValuesForKeysWithDictionary:(id)objectData
[self MR_addObject:relatedObject forRelationship:relationshipInfo];
}];
- swizzle([objectData class], @selector(valueForUndefinedKey:), @selector(MR_valueForUndefinedKey:));
+ MR_swizzle([objectData class], @selector(valueForUndefinedKey:), @selector(MR_valueForUndefinedKey:));
if ([self respondsToSelector:@selector(didImport)])
{
@@ -277,7 +281,7 @@ - (void) MR_updateValuesForKeysWithDictionary:(id)objectData
+ (id) MR_importFromDictionary:(id)objectData inContext:(NSManagedObjectContext *)context;
{
NSManagedObject *managedObject = [self MR_createInContext:context];
- [managedObject MR_importValuesForKeysWithDictionary:objectData];
+ [managedObject MR_importValuesForKeysWithObject:objectData];
return managedObject;
}
@@ -296,7 +300,7 @@ + (id) MR_updateFromDictionary:(id)objectData inContext:(NSManagedObjectContext
if (!managedObject)
{
managedObject = [self MR_createInContext:context];
- [managedObject MR_importValuesForKeysWithDictionary:objectData];
+ [managedObject MR_importValuesForKeysWithObject:objectData];
}
else
{
@@ -319,7 +323,7 @@ + (NSArray *) MR_importFromArray:(NSArray *)listOfObjectData inContext:(NSManage
{
NSMutableArray *objectIDs = [NSMutableArray array];
- [MRCoreDataAction saveDataWithBlock:^(NSManagedObjectContext *localContext)
+ [MagicalRecord saveDataWithBlock:^(NSManagedObjectContext *localContext)
{
[listOfObjectData enumerateObjectsWithOptions:0 usingBlock:^(id obj, NSUInteger idx, BOOL *stop)
{
@@ -346,7 +350,7 @@ + (NSArray *) MR_updateFromArray:(NSArray *)listOfObjectData inContext:(NSManage
{
NSMutableArray *objectIDs = [NSMutableArray array];
- [MRCoreDataAction saveDataWithBlock:^(NSManagedObjectContext *localContext)
+ [MagicalRecord saveDataWithBlock:^(NSManagedObjectContext *localContext)
{
[listOfObjectData enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
@@ -367,7 +371,7 @@ + (NSArray *) MR_updateFromArray:(NSArray *)listOfObjectData inContext:(NSManage
@end
-void swizzle(Class c, SEL orig, SEL new)
+void MR_swizzle(Class c, SEL orig, SEL new)
{
Method origMethod = class_getInstanceMethod(c, orig);
Method newMethod = class_getInstanceMethod(c, new);
@@ -5,7 +5,7 @@
//
#import <CoreData/CoreData.h>
-#import "MagicalRecordHelpers.h"
+#import "MagicalRecord.h"
#define kMagicalRecordDefaultBatchSize 20
@@ -31,7 +31,7 @@ + (NSArray *) MR_executeFetchRequest:(NSFetchRequest *)request inContext:(NSMana
if (results == nil)
{
- [MagicalRecordHelpers handleErrors:error];
+ [MagicalRecord handleErrors:error];
}
return results;
}
@@ -65,7 +65,7 @@ + (void) MR_performFetch:(NSFetchedResultsController *)controller
NSError *error = nil;
if (![controller performFetch:&error])
{
- [MagicalRecordHelpers handleErrors:error];
+ [MagicalRecord handleErrors:error];
}
}
@@ -128,7 +128,6 @@ + (NSArray *) MR_sortAscending:(BOOL)ascending attributes:(NSArray *)attributesT
{
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:attributeName ascending:ascending];
[attributes addObject:sortDescriptor];
- MR_RELEASE(sortDescriptor);
}
return attributes;
@@ -148,7 +147,6 @@ + (NSFetchRequest *)MR_createFetchRequestInContext:(NSManagedObjectContext *)con
{
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[self MR_entityDescriptionInContext:context]];
- MR_AUTORELEASE(request);
return request;
}
@@ -192,7 +190,7 @@ + (NSUInteger) MR_countOfEntitiesWithContext:(NSManagedObjectContext *)context;
{
NSError *error = nil;
NSUInteger count = [context countForFetchRequest:[self MR_createFetchRequestInContext:context] error:&error];
- [MagicalRecordHelpers handleErrors:error];
+ [MagicalRecord handleErrors:error];
return count;
}
@@ -209,7 +207,7 @@ + (NSUInteger) MR_countOfEntitiesWithPredicate:(NSPredicate *)searchFilter inCon
[request setPredicate:searchFilter];
NSUInteger count = [context countForFetchRequest:request error:&error];
- [MagicalRecordHelpers handleErrors:error];
+ [MagicalRecord handleErrors:error];
return count;
}
@@ -295,7 +293,6 @@ + (NSFetchRequest *) MR_requestAllSortedBy:(NSString *)sortTerm ascending:(BOOL)
NSSortDescriptor *sortBy = [[NSSortDescriptor alloc] initWithKey:sortTerm ascending:ascending];
[request setSortDescriptors:[NSArray arrayWithObject:sortBy]];
- MR_AUTORELEASE(sortBy);
return request;
}
@@ -319,11 +316,9 @@ + (NSFetchRequest *) MR_requestAllSortedBy:(NSString *)sortTerm ascending:(BOOL)
{
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:sortKey ascending:ascending];
[sortDescriptors addObject:sortDescriptor];
- MR_AUTORELEASE(sortDescriptor);
}
[request setSortDescriptors:sortDescriptors];
- MR_AUTORELEASE(sortDescriptors);
return request;
}
@@ -398,7 +393,6 @@ + (NSFetchedResultsController *) MR_fetchController:(NSFetchRequest *)request de
sectionNameKeyPath:groupKeyPath
cacheName:cacheName];
controller.delegate = delegate;
- MR_AUTORELEASE(controller);
return controller;
}
@@ -745,7 +739,6 @@ + (NSNumber *) MR_aggregateOperation:(NSString *)function onAttribute:(NSString
NSAttributeDescription *attributeDescription = [[[self MR_entityDescription] attributesByName] objectForKey:attributeName];
[ed setExpressionResultType:[attributeDescription attributeType]];
NSArray *properties = [NSArray arrayWithObject:ed];
- MR_RELEASE(ed);
NSFetchRequest *request = [self MR_requestAllWithPredicate:predicate inContext:context];
[request setPropertiesToFetch:properties];
@@ -769,7 +762,7 @@ - (id) MR_inContext:(NSManagedObjectContext *)otherContext
{
NSError *error = nil;
NSManagedObject *inContext = [otherContext existingObjectWithID:[self objectID] error:&error];
- [MagicalRecordHelpers handleErrors:error];
+ [MagicalRecord handleErrors:error];
return inContext;
}
@@ -5,7 +5,7 @@
// Copyright 2010 Magical Panda Software, LLC All rights reserved.
//
-#import "MagicalRecordHelpers.h"
+#import "MagicalRecord.h"
extern NSString * const kMagicalRecordDidMergeChangesFromiCloudNotification;
Oops, something went wrong.

0 comments on commit df24272

Please sign in to comment.