forked from RestKit/RestKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Break out reused functions for object mapping introspection into RKOb…
…jectUtilities
- Loading branch information
1 parent
03439f2
commit d95fe5c
Showing
8 changed files
with
179 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// | ||
// RKObjectUtilities.h | ||
// RestKit | ||
// | ||
// Created by Blake Watters on 9/30/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 <Foundation/Foundation.h> | ||
|
||
///---------------- | ||
/// @name Functions | ||
///---------------- | ||
|
||
/** | ||
Returns a Boolean value that indicates whether the given objects are equal. | ||
The actual method of comparison is dependendent upon the class of the objects given. For example, given two `NSString` objects equality would be tested using `isEqualToString:`. | ||
@param object The first object to compare. | ||
@param anotherObject The second object to compare. | ||
@return `YES` if the objects are equal, otherwise `NO`. | ||
*/ | ||
BOOL RKObjectIsEqualToObject(id object, id anotherObject); | ||
|
||
/** | ||
Returns a Boolean value that indicates if the given class is a collection. | ||
The following classes are considered collections: | ||
1. `NSSet` | ||
1. `NSArray` | ||
1. `NSOrderedSet` | ||
`NSDictionary` objects are **not** considered collections as they are typically object representations. | ||
@param aClass The class to check. | ||
@return `YES` if the given class is a collection. | ||
*/ | ||
BOOL RKClassIsCollection(Class aClass); | ||
|
||
/** | ||
Returns a Boolean value that indicates if the given object is a collection. | ||
Implemented by invoking `RKClassIsCollection` with the class of the given object. | ||
@param object The object to be tested. | ||
@return `YES` if the given object is a collection, else `NO`. | ||
@see `RKClassIsCollection` | ||
*/ | ||
BOOL RKObjectIsCollection(id object); | ||
|
||
/** | ||
Returns a Boolean value that indicates if the given object is collection containing only instances of `NSManagedObject` or a class that inherits from `NSManagedObject`. | ||
@param object The object to be tested. | ||
@return `YES` if the object is a collection containing only `NSManagedObject` derived objects. | ||
*/ | ||
BOOL RKObjectIsCollectionContainingOnlyManagedObjects(id object); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// | ||
// RKObjectUtilities.m | ||
// RestKit | ||
// | ||
// Created by Blake Watters on 9/30/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 <objc/message.h> | ||
#import "RKObjectUtilities.h" | ||
|
||
BOOL RKObjectIsEqualToObject(id object, id anotherObject) { | ||
NSCAssert(object, @"Expected object not to be nil"); | ||
NSCAssert(anotherObject, @"Expected anotherObject not to be nil"); | ||
|
||
SEL comparisonSelector; | ||
if ([object isKindOfClass:[NSString class]] && [anotherObject isKindOfClass:[NSString class]]) { | ||
comparisonSelector = @selector(isEqualToString:); | ||
} else if ([object isKindOfClass:[NSNumber class]] && [anotherObject isKindOfClass:[NSNumber class]]) { | ||
comparisonSelector = @selector(isEqualToNumber:); | ||
} else if ([object isKindOfClass:[NSDate class]] && [anotherObject isKindOfClass:[NSDate class]]) { | ||
comparisonSelector = @selector(isEqualToDate:); | ||
} else if ([object isKindOfClass:[NSArray class]] && [anotherObject isKindOfClass:[NSArray class]]) { | ||
comparisonSelector = @selector(isEqualToArray:); | ||
} else if ([object isKindOfClass:[NSDictionary class]] && [anotherObject isKindOfClass:[NSDictionary class]]) { | ||
comparisonSelector = @selector(isEqualToDictionary:); | ||
} else if ([object isKindOfClass:[NSSet class]] && [anotherObject isKindOfClass:[NSSet class]]) { | ||
comparisonSelector = @selector(isEqualToSet:); | ||
} else { | ||
comparisonSelector = @selector(isEqual:); | ||
} | ||
|
||
// Comparison magic using function pointers. See this page for details: http://www.red-sweater.com/blog/320/abusing-objective-c-with-class | ||
// Original code courtesy of Greg Parker | ||
// This is necessary because isEqualToNumber will return negative integer values that aren't coercable directly to BOOL's without help [sbw] | ||
BOOL (*ComparisonSender)(id, SEL, id) = (BOOL (*)(id, SEL, id))objc_msgSend; | ||
return ComparisonSender(object, comparisonSelector, anotherObject); | ||
} | ||
|
||
BOOL RKClassIsCollection(Class aClass) | ||
{ | ||
return (aClass && ([aClass isSubclassOfClass:[NSSet class]] || | ||
[aClass isSubclassOfClass:[NSArray class]] || | ||
[aClass isSubclassOfClass:[NSOrderedSet class]])); | ||
} | ||
|
||
BOOL RKObjectIsCollection(id object) | ||
{ | ||
return RKClassIsCollection([object class]); | ||
} | ||
|
||
BOOL RKObjectIsCollectionContainingOnlyManagedObjects(id object) | ||
{ | ||
if (! RKObjectIsCollection(object)) return NO; | ||
Class managedObjectClass = NSClassFromString(@"NSManagedObject"); | ||
if (! managedObjectClass) return NO; | ||
for (id instance in object) { | ||
if (! [object isKindOfClass:managedObjectClass]) return NO; | ||
} | ||
return YES; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.