Permalink
Browse files

Merge branch 'release/0.20.0-pre2'

  • Loading branch information...
blakewatters committed Dec 2, 2012
2 parents 17996ad + 971083e commit f62f6aca119426fd36f717e5ab05884be2352dd9
@@ -270,7 +270,7 @@ - (void)addObject:(NSManagedObject *)object
__block NSEntityDescription *entity;
__block NSDictionary *attributeValues;
__block NSManagedObjectID *objectID;
- [self.managedObjectContext performBlockAndWait:^{
+ [object.managedObjectContext performBlockAndWait:^{
entity = object.entity;
objectID = [object objectID];
attributeValues = [object dictionaryWithValuesForKeys:self.attributes];
@@ -1,104 +0,0 @@
-//
-// RKEntityIdentifier.h
-// RestKit
-//
-// Created by Blake Watters on 11/20/12.
-// Copyright (c) 2012 RestKit. All rights reserved.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-
-#import <CoreData/CoreData.h>
-
-/**
- The name of a key in the user info dictionary of a `NSEntityDescription` specifying the name or one or more attributes to be used to infer an entity identifier. The value of this string is 'RKEntityIdentifierAttributes'.
- */
-extern NSString * const RKEntityIdentifierUserInfoKey;
-
-@class RKManagedObjectStore;
-
-/**
- The `RKEntityIdentifier` object describes a means for uniquely identifying one or more `NSManagedObject` objects within a Core Data managed object model. Entity identifiers are used by RestKit to identify existing managed objects that are to be updated while performing object mapping with an `RKEntityMapping` object. Entity identifiers identify objects by specifying the attributes that can be used to uniquely differentiate managed objects within a context.
- */
-@interface RKEntityIdentifier : NSObject
-
-///----------------------------------------
-/// @name Initializing an Entity Identifier
-///----------------------------------------
-
-/**
- Creates and returns a new entity identifier with the entity for the given name and attributes in the specified managed object store.
-
- @param entityName The name of the entity being identified.
- @param attributes An array of `NSString` objects containing the names of attributes or `NSAttributeDescription` objects specifying the identifying attributes for the entity.
- @param managedObjectStore The managed object store containing the managed object model within which the entity exists.
- @return A new entity identifier, configured with the given entity for the given name and the array of attributes.
- */
-+ (id)identifierWithEntityName:(NSString *)entityName attributes:(NSArray *)attributes inManagedObjectStore:(RKManagedObjectStore *)managedObjectStore;
-
-/**
- Initializes the receiver with a given entity and array of attributes.
-
- @param entity The entity being identified.
- @param attributes An array of `NSString` objects containing the names of attributes or `NSAttributeDescription` objects specifying the identifying attributes for the entity.
- @return The receiver, initialized with the given entity and attributes.
- */
-- (id)initWithEntity:(NSEntityDescription *)entity attributes:(NSArray *)attributes;
-
-///--------------------------------
-/// @name Accessing Entity Identity
-///--------------------------------
-
-/**
- The entity that the receiver identifies.
- */
-@property (nonatomic, strong, readonly) NSEntityDescription *entity;
-
-/**
- An array of `NSAttributeDescription` objects specifying the attributes used for identification.
- */
-@property (nonatomic, copy, readonly) NSArray *attributes;
-
-///---------------------------------------
-/// @name Filtering the Identified Objects
-///---------------------------------------
-
-/**
- An optional predicate for filtering identified objects.
- */
-@property (nonatomic, copy) NSPredicate *predicate;
-
-///-------------------------------------------
-/// @name Inferring Identifiers from the Model
-///-------------------------------------------
-
-/**
- Creates and returns an entity identifier for the given entity inferred from the managed object model.
-
- When inferring an entity identifier, the entity is first checked for a user info key specifying the identifying attributes. If the user info of the given entity contains a value for the key 'RKEntityIdentifierAttributes', then that value is used to construct an entity identifier. The user info key must contain a string or an array of strings specifying the names of attributes that exist in the given entity.
-
- If no attributes are specified in the user info, then the entity is searched for an attribute whose name matches the llama-cased name of the entity. For example, an entity named 'Article' would have an inferred identifier attribute of 'articleID' and an entity named 'ApprovedComment' would be inferred as 'approvedCommentID'. If such an attribute is found within the entity, an identifier is returned specifying that attribute. If none is returned, the the attributes are search for the following names:
-
- 1. 'identifier'
- 1. 'ID'
- 1. 'URL'
- 1. 'url'
-
- If any of these attributes are found, then an entity identifier is created for that attribute. If all possible inferred attributes are exhausted, then `nil` is returned.
-
- @param entity The entity to infer an identifier for.
- @return An entity identifier inferred from the model for the given entity, or `nil` if none could be inferred.
- */
-+ (RKEntityIdentifier *)inferredIdentifierForEntity:(NSEntityDescription *)entity;
-
-@end
@@ -1,129 +0,0 @@
-//
-// RKEntityIdentifier.m
-// RestKit
-//
-// Created by Blake Watters on 11/20/12.
-// Copyright (c) 2012 RestKit. All rights reserved.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-
-#import "RKEntityIdentifier.h"
-#import "RKManagedObjectStore.h"
-
-NSString * const RKEntityIdentifierUserInfoKey = @"RKEntityIdentifierAttributes";
-
-static NSArray *RKEntityIdentifierAttributesFromUserInfoOfEntity(NSEntityDescription *entity)
-{
- id userInfoValue = [entity userInfo][RKEntityIdentifierUserInfoKey];
- if (userInfoValue) {
- NSArray *attributeNames = [userInfoValue isKindOfClass:[NSArray class]] ? userInfoValue : @[ userInfoValue ];
- NSMutableArray *attributes = [NSMutableArray arrayWithCapacity:[attributeNames count]];
- [attributeNames enumerateObjectsUsingBlock:^(NSString *attributeName, NSUInteger idx, BOOL *stop) {
- if (! [attributeName isKindOfClass:[NSString class]]) {
- [NSException raise:NSInvalidArgumentException format:@"Invalid value given in user info key '%@' of entity '%@': expected an `NSString` or `NSArray` of strings, instead got '%@' (%@)", RKEntityIdentifierUserInfoKey, [entity name], attributeName, [attributeName class]];
- }
-
- NSAttributeDescription *attribute = [entity attributesByName][attributeName];
- if (! attribute) {
- [NSException raise:NSInvalidArgumentException format:@"Invalid identifier attribute specified in user info key '%@' of entity '%@': no attribue was found with the name '%@'", RKEntityIdentifierUserInfoKey, [entity name], attributeName];
- }
-
- [attributes addObject:attribute];
- }];
- return attributes;
- }
-
- return nil;
-}
-
-// Given 'Human', returns 'humanID'; Given 'AmenityReview' returns 'amenityReviewID'
-static NSString *RKEntityIdentifierAttributeNameForEntity(NSEntityDescription *entity)
-{
- NSString *entityName = [entity name];
- NSString *lowerCasedFirstCharacter = [[entityName substringToIndex:1] lowercaseString];
- return [NSString stringWithFormat:@"%@%@ID", lowerCasedFirstCharacter, [entityName substringFromIndex:1]];
-}
-
-static NSArray *RKEntityIdentifierAttributeNames()
-{
- return [NSArray arrayWithObjects:@"identifier", @"ID", @"URL", @"url", nil];
-}
-
-static NSArray *RKArrayOfAttributesForEntityFromAttributesOrNames(NSEntityDescription *entity, NSArray *attributesOrNames)
-{
- NSMutableArray *attributes = [NSMutableArray arrayWithCapacity:[attributesOrNames count]];
- for (id attributeOrName in attributesOrNames) {
- if ([attributeOrName isKindOfClass:[NSAttributeDescription class]]) {
- if (! [[entity properties] containsObject:attributeOrName]) [NSException raise:NSInvalidArgumentException format:@"Invalid attribute value '%@' given for entity identifer: not found in the '%@' entity", attributeOrName, [entity name]];
- [attributes addObject:attributeOrName];
- } else if ([attributeOrName isKindOfClass:[NSString class]]) {
- NSAttributeDescription *attribute = [entity attributesByName][attributeOrName];
- if (!attribute) [NSException raise:NSInvalidArgumentException format:@"Invalid attribute '%@': no attribute was found for the given name in the '%@' entity.", attributeOrName, [entity name]];
- [attributes addObject:attribute];
- } else {
- [NSException raise:NSInvalidArgumentException format:@"Invalid value provided for entity identifier attribute: Acceptable values are either `NSAttributeDescription` or `NSString` objects."];
- }
- }
-
- return attributes;
-}
-
-@interface RKEntityIdentifier ()
-@property (nonatomic, strong, readwrite) NSEntityDescription *entity;
-@property (nonatomic, copy, readwrite) NSArray *attributes;
-@end
-
-@implementation RKEntityIdentifier
-
-+ (id)identifierWithEntityName:(NSString *)entityName attributes:(NSArray *)attributes inManagedObjectStore:(RKManagedObjectStore *)managedObjectStore
-{
- NSEntityDescription *entity = [managedObjectStore.managedObjectModel entitiesByName][entityName];
- return [[self alloc] initWithEntity:entity attributes:attributes];
-}
-
-- (id)initWithEntity:(NSEntityDescription *)entity attributes:(NSArray *)attributes
-{
- NSParameterAssert(entity);
- NSParameterAssert(attributes);
- NSAssert([attributes count], @"At least one attribute must be provided to identify managed objects");
- self = [self init];
- if (self) {
- self.entity = entity;
- self.attributes = RKArrayOfAttributesForEntityFromAttributesOrNames(entity, attributes);
- }
-
- return self;
-}
-
-#pragma mark - Idetifier Inference
-
-+ (RKEntityIdentifier *)inferredIdentifierForEntity:(NSEntityDescription *)entity
-{
- NSArray *attributes = RKEntityIdentifierAttributesFromUserInfoOfEntity(entity);
- if (attributes) {
- return [[RKEntityIdentifier alloc] initWithEntity:entity attributes:attributes];
- }
-
- NSMutableArray *identifyingAttributes = [NSMutableArray arrayWithObject:RKEntityIdentifierAttributeNameForEntity(entity)];
- [identifyingAttributes addObjectsFromArray:RKEntityIdentifierAttributeNames()];
- for (NSString *attributeName in identifyingAttributes) {
- NSAttributeDescription *attribute = [entity attributesByName][attributeName];
- if (attribute) {
- return [[RKEntityIdentifier alloc] initWithEntity:entity attributes:@[ attribute ]];
- }
- }
- return nil;
-}
-
-@end
Oops, something went wrong.

0 comments on commit f62f6ac

Please sign in to comment.