Skip to content

Commit

Permalink
Merge branch 'release/0.10.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
blakewatters committed May 26, 2012
2 parents a999a63 + 8647fa5 commit f11a538
Show file tree
Hide file tree
Showing 303 changed files with 12,864 additions and 6,391 deletions.
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,4 @@ Docs/API

Examples/RKDiscussionBoardExample/discussion_board_backend/public/system/attachments/*

# Thin
log
tmp
test-reports/
8 changes: 4 additions & 4 deletions Code/CoreData/CoreData.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
//
// Created by Blake Watters on 9/30/10.
// Copyright (c) 2009-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.
Expand All @@ -25,7 +25,7 @@
#import "RKManagedObjectSeeder.h"
#import "RKManagedObjectMapping.h"
#import "RKManagedObjectMappingOperation.h"
#import "RKManagedObjectMappingCache.h"
#import "RKManagedObjectCaching.h"
#import "RKInMemoryManagedObjectCache.h"
#import "RKFetchRequestManagedObjectCache.h"
#import "RKSearchableManagedObject.h"
Expand Down
56 changes: 55 additions & 1 deletion Code/CoreData/NSEntityDescription+RKAdditions.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@
*/
extern NSString * const RKEntityDescriptionPrimaryKeyAttributeUserInfoKey;

/**
The substitution variable used in predicateForPrimaryKeyAttribute.
**Value**: @"PRIMARY_KEY_VALUE"
@see predicateForPrimaryKeyAttribute
*/
extern NSString * const RKEntityDescriptionPrimaryKeyAttributeValuePredicateSubstitutionVariable;

/**
Provides extensions to NSEntityDescription for various common tasks.
*/
Expand All @@ -34,6 +42,52 @@ extern NSString * const RKEntityDescriptionPrimaryKeyAttributeUserInfoKey;
Programmatically configured values take precedence over the user info
dictionary.
*/
@property(nonatomic, retain) NSString *primaryKeyAttribute;
@property (nonatomic, retain) NSString *primaryKeyAttributeName;

/**
The attribute description object for the attribute designated as the primary key for the receiver.
*/
@property (nonatomic, readonly) NSAttributeDescription *primaryKeyAttribute;

/**
The class representing the value of the attribute designated as the primary key for the receiver.
*/
@property (nonatomic, readonly) Class primaryKeyAttributeClass;

/**
Returns a cached predicate specifying that the primary key attribute is equal to the $PRIMARY_KEY_VALUE
substitution variable.
This predicate is cached to avoid parsing overhead during object mapping operations
and must be evaluated using [NSPredicate predicateWithSubstitutionVariables:]
@return A cached predicate specifying the value of the primary key attribute is equal to the $PRIMARY_KEY_VALUE
substitution variable.
*/
- (NSPredicate *)predicateForPrimaryKeyAttribute;

/**
Returns a predicate specifying that the value of the primary key attribute is equal to a given
value. This predicate is constructed by evaluating the cached predicate returned by the
predicateForPrimaryKeyAttribute with a dictionary of substitution variables specifying that
$PRIMARY_KEY_VALUE is equal to the given value.
**NOTE**: This method considers the type of the receiver's primary key attribute when constructing
the predicate. It will coerce the given value into either an NSString or an NSNumber as
appropriate. This behavior is a convenience to avoid annoying issues related to Core Data's
handling of predicates for NSString and NSNumber types that were not appropriately casted.
@return A predicate speciying that the value of the primary key attribute is equal to a given value.
*/
- (NSPredicate *)predicateForPrimaryKeyAttributeWithValue:(id)value;

/**
Coerces the given value into the class representing the primary key. Currently support NSString
and NSNumber coercsions.
@bug **NOTE** This API is temporary and will be deprecated and replaced.
@since 0.10.1
*/
- (id)coerceValueForPrimaryKey:(id)primaryKeyValue;

@end
81 changes: 75 additions & 6 deletions Code/CoreData/NSEntityDescription+RKAdditions.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,98 @@
#import "NSEntityDescription+RKAdditions.h"

NSString * const RKEntityDescriptionPrimaryKeyAttributeUserInfoKey = @"primaryKeyAttribute";
static char primaryKeyAttributeKey;
NSString * const RKEntityDescriptionPrimaryKeyAttributeValuePredicateSubstitutionVariable = @"PRIMARY_KEY_VALUE";

static char primaryKeyAttributeNameKey, primaryKeyPredicateKey;

@implementation NSEntityDescription (RKAdditions)

- (NSString *)primaryKeyAttribute
- (void)setPredicateForPrimaryKeyAttribute:(NSString *)primaryKeyAttribute
{
NSPredicate *predicate = (primaryKeyAttribute) ? [NSPredicate predicateWithFormat:@"%K == $PRIMARY_KEY_VALUE", primaryKeyAttribute] : nil;
objc_setAssociatedObject(self,
&primaryKeyPredicateKey,
predicate,
OBJC_ASSOCIATION_RETAIN);
}

#pragma mark - Public

- (NSAttributeDescription *)primaryKeyAttribute
{
return [[self attributesByName] valueForKey:[self primaryKeyAttributeName]];
}

- (Class)primaryKeyAttributeClass
{
NSAttributeDescription *attributeDescription = [self primaryKeyAttribute];
if (attributeDescription) {
return NSClassFromString(attributeDescription.attributeValueClassName);
}

return nil;
}

- (NSString *)primaryKeyAttributeName
{
// Check for an associative object reference
NSString *primaryKeyAttribute = (NSString *) objc_getAssociatedObject(self, &primaryKeyAttributeKey);
NSString *primaryKeyAttribute = (NSString *) objc_getAssociatedObject(self, &primaryKeyAttributeNameKey);

// Fall back to the userInfo dictionary
if (! primaryKeyAttribute) {
primaryKeyAttribute = [self.userInfo valueForKey:RKEntityDescriptionPrimaryKeyAttributeUserInfoKey];

// If we have loaded from the user info, ensure we have a predicate
if (! [self predicateForPrimaryKeyAttribute]) {
[self setPredicateForPrimaryKeyAttribute:primaryKeyAttribute];
}
}

return primaryKeyAttribute;
}

- (void)setPrimaryKeyAttribute:(NSString *)primaryKeyAttribute
- (void)setPrimaryKeyAttributeName:(NSString *)primaryKeyAttributeName
{
objc_setAssociatedObject(self,
&primaryKeyAttributeKey,
primaryKeyAttribute,
&primaryKeyAttributeNameKey,
primaryKeyAttributeName,
OBJC_ASSOCIATION_RETAIN);
[self setPredicateForPrimaryKeyAttribute:primaryKeyAttributeName];
}

- (NSPredicate *)predicateForPrimaryKeyAttribute
{
return (NSPredicate *) objc_getAssociatedObject(self, &primaryKeyPredicateKey);
}

- (id)coerceValueForPrimaryKey:(id)primaryKeyValue
{
id searchValue = primaryKeyValue;
Class theClass = [self primaryKeyAttributeClass];
if (theClass) {
// TODO: This coercsion behavior should be pluggable and reused from the mapper
if ([theClass isSubclassOfClass:[NSNumber class]] && ![searchValue isKindOfClass:[NSNumber class]]) {
// Handle NSString -> NSNumber
if ([searchValue isKindOfClass:[NSString class]]) {
searchValue = [NSNumber numberWithDouble:[searchValue doubleValue]];
}
} else if ([theClass isSubclassOfClass:[NSString class]] && ![searchValue isKindOfClass:[NSString class]]) {
// Coerce to string
if ([searchValue respondsToSelector:@selector(stringValue)]) {
searchValue = [searchValue stringValue];
}
}
}

return searchValue;
}

- (NSPredicate *)predicateForPrimaryKeyAttributeWithValue:(id)value
{
id substitutionValue = [self coerceValueForPrimaryKey:value];
NSDictionary *variables = [NSDictionary dictionaryWithObject:substitutionValue
forKey:RKEntityDescriptionPrimaryKeyAttributeValuePredicateSubstitutionVariable];
return [[self predicateForPrimaryKeyAttribute] predicateWithSubstitutionVariables:variables];
}

@end
8 changes: 4 additions & 4 deletions Code/CoreData/NSManagedObject+ActiveRecord.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@
@interface NSManagedObject (ActiveRecord)

/**
* The NSEntityDescription for the Subclass
* defaults to the subclass className, may be overridden
* The NSEntityDescription for the Subclass
* defaults to the subclass className, may be overridden
*/
+ (NSEntityDescription*)entity;

/**
* Returns an initialized NSFetchRequest for the entity, with no predicate
* Returns an initialized NSFetchRequest for the entity, with no predicate
*/
+ (NSFetchRequest*)fetchRequest;

Expand Down Expand Up @@ -94,7 +94,7 @@
+ (NSUInteger)count DEPRECATED_ATTRIBUTE;

/**
* Creates a new managed object and inserts it into the managedObjectContext.
* Creates a new managed object and inserts it into the managedObjectContext.
*/
+ (id)object;

Expand Down
Loading

0 comments on commit f11a538

Please sign in to comment.