Misc updates to RKTestFactory

* Add `objectFromFactory:properties:` this may deprecate `objectFromFactory:` in the future.
* Add `insertManagedObjectForEntityForName:inManagedObjectContext:withProperties:`. This helper method creates managed object instances in a given context (defaulting to the main queue context), sets its properties using an optional dictionary, and obtains a permanent `NSManagedObjectID` before returning. This is intended to reduce the boilerplate required to construct managed object factories.
This commit is contained in:
Blake Watters
2012-09-12 16:29:46 -04:00
parent 3645d73312
commit 3afc7c3501
2 changed files with 50 additions and 9 deletions

View File

@@ -145,15 +145,28 @@ extern NSString * const RKTestFactoryDefaultNamesManagedObjectStore;
+ (void)defineFactory:(NSString *)factoryName withBlock:(id (^)())block;
/**
Creates and returns a new instance of an object using the factory with the given
name.
Creates and returns a new instance of an object using the factory with the given name.
@param factoryName The name of the factory to use when building the requested object.
@raises NSInvalidArgumentException Raised if a factory with the given name is not defined.
@param properties An `NSDictionary` of properties to be set on the created object.
@return An object built using the factory registered for the given name.
*/
+ (id)objectFromFactory:(NSString *)factoryName properties:(NSDictionary *)properties;
+ (id)objectFromFactory:(NSString *)factoryName;
/**
Inserts a new managed object for the `NSEntityDescription` with the given name into the specified managed object context and sets properties on the instance from the given dictionary. A permanent managed object ID is obtained for the object so that it can be referenced across threads without any further work.
@param entityName The name of the entity to insert a new managed object for.
@param managedObjectContext The managed object context to insert the new object into. If nil, then the managed object context returned by invoking `[RKTestFactory managedObjectStore].mainQueueManagedObjectContext]` is used.
@param properties A dictionary of properties to be set on the new managed object instance.
@return A new object inheriting from `NSManagedObject`.
*/
+ (id)insertManagedObjectForEntityForName:(NSString *)entityName
inManagedObjectContext:(NSManagedObjectContext *)managedObjectContext
withProperties:(NSDictionary *)properties;
/**
Returns a set of names for all defined factories.

View File

@@ -17,7 +17,7 @@
+ (RKTestFactory *)sharedFactory;
- (void)defineFactory:(NSString *)factoryName withBlock:(id (^)())block;
- (id)objectFromFactory:(NSString *)factoryName;
- (id)objectFromFactory:(NSString *)factoryName properties:(NSDictionary *)properties;
- (void)defineDefaultFactories;
@end
@@ -64,12 +64,14 @@ static RKTestFactory *sharedFactory = nil;
[self.factoryBlocks setObject:[block copy] forKey:factoryName];
}
- (id)objectFromFactory:(NSString *)factoryName
- (id)objectFromFactory:(NSString *)factoryName properties:(NSDictionary *)properties
{
id (^block)() = [self.factoryBlocks objectForKey:factoryName];
NSAssert(block, @"No factory is defined with the name '%@'", factoryName);
return block();
id object = block();
[object setValuesForKeysWithDictionary:properties];
return object;
}
- (void)defineDefaultFactories
@@ -151,9 +153,35 @@ static RKTestFactory *sharedFactory = nil;
[[RKTestFactory sharedFactory] defineFactory:factoryName withBlock:block];
}
+ (id)objectFromFactory:(NSString *)factoryName properties:(NSDictionary *)properties
{
return [[RKTestFactory sharedFactory] objectFromFactory:factoryName properties:properties];
}
+ (id)objectFromFactory:(NSString *)factoryName
{
return [[RKTestFactory sharedFactory] objectFromFactory:factoryName];
return [[RKTestFactory sharedFactory] objectFromFactory:factoryName properties:nil];
}
+ (id)insertManagedObjectForEntityForName:(NSString *)entityName
inManagedObjectContext:(NSManagedObjectContext *)managedObjectContext
withProperties:(NSDictionary *)properties
{
__block id managedObject;
__block NSError *error;
__block BOOL success;
__block NSManagedObjectID *objectID;
if (! managedObjectContext) managedObjectContext = [[RKTestFactory managedObjectStore] mainQueueManagedObjectContext];
[managedObjectContext performBlockAndWait:^{
managedObject = [NSEntityDescription insertNewObjectForEntityForName:entityName inManagedObjectContext:managedObjectContext];
success = [managedObjectContext obtainPermanentIDsForObjects:@[managedObject] error:&error];
if (! success) {
RKLogWarning(@"Failed to obtain permanent objectID for managed object: %@", managedObject);
RKLogCoreDataError(error);
}
[managedObject setValuesForKeysWithDictionary:properties];
}];
return managedObject;
}
+ (NSSet *)factoryNames
@@ -163,20 +191,20 @@ static RKTestFactory *sharedFactory = nil;
+ (id)client
{
AFHTTPClient *client = [self objectFromFactory:RKTestFactoryDefaultNamesClient];
AFHTTPClient *client = [self objectFromFactory:RKTestFactoryDefaultNamesClient properties:nil];
return client;
}
+ (id)objectManager
{
RKObjectManager *objectManager = [self objectFromFactory:RKTestFactoryDefaultNamesObjectManager];
RKObjectManager *objectManager = [self objectFromFactory:RKTestFactoryDefaultNamesObjectManager properties:nil];
[RKObjectManager setSharedManager:objectManager];
return objectManager;
}
+ (id)managedObjectStore
{
RKManagedObjectStore *managedObjectStore = [self objectFromFactory:RKTestFactoryDefaultNamesManagedObjectStore];
RKManagedObjectStore *managedObjectStore = [self objectFromFactory:RKTestFactoryDefaultNamesManagedObjectStore properties:nil];
[RKManagedObjectStore setDefaultStore:managedObjectStore];
return managedObjectStore;