Skip to content

Commit da82149

Browse files
author
Richard Piazza
committed
Singularization
Better handling of Singularization; Replacing module name spaces with underscores.
1 parent 87f05d8 commit da82149

File tree

4 files changed

+58
-80
lines changed

4 files changed

+58
-80
lines changed

CodeQuickKit-ObjC/CQKSerializableNSManagedObject.m

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -340,45 +340,7 @@ - (NSObject *)serializedObjectForPropertyName:(NSString *)propertyName withData:
340340

341341
- (Class)objectClassOfCollectionTypeForPropertyName:(NSString *)propertyName
342342
{
343-
if (propertyName == nil || [propertyName isEqualToString:@""]) {
344-
return [NSNull class];
345-
}
346-
347-
Class entityClass = NSClassFromString(propertyName);
348-
if (entityClass != nil) {
349-
return entityClass;
350-
}
351-
352-
NSMutableString *singular = [propertyName mutableCopy];
353-
if ([singular.lowercaseString hasSuffix:@"s"]) {
354-
[singular replaceCharactersInRange:NSMakeRange(singular.length - 1, 1) withString:@""];
355-
}
356-
357-
[singular replaceCharactersInRange:NSMakeRange(0, 1) withString:[singular substringToIndex:1].uppercaseString];
358-
359-
entityClass = NSClassFromString(singular);
360-
if (entityClass != nil) {
361-
return entityClass;
362-
}
363-
364-
NSBundle *bundle = [NSBundle bundleForClass:self.class];
365-
if (bundle.bundleDisplayName != nil) {
366-
NSString *moduleName = [NSString stringWithFormat:@"%@.%@", bundle.bundleDisplayName, singular];
367-
entityClass = NSClassFromString(moduleName);
368-
if (entityClass != nil) {
369-
return entityClass;
370-
}
371-
}
372-
373-
if (bundle.bundleName != nil) {
374-
NSString *moduleName = [NSString stringWithFormat:@"%@.%@", bundle.bundleName, singular];
375-
entityClass = NSClassFromString(moduleName);
376-
if (entityClass != nil) {
377-
return entityClass;
378-
}
379-
}
380-
381-
return [NSNull class];
343+
return [NSObject singularizedClassForPropertyName:propertyName];
382344
}
383345

384346
#pragma mark -

CodeQuickKit-ObjC/CQKSerializableNSObject.m

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -355,45 +355,7 @@ - (NSObject *)serializedObjectForPropertyName:(NSString *)propertyName withData:
355355

356356
- (Class)objectClassOfCollectionTypeForPropertyName:(NSString *)propertyName
357357
{
358-
if (propertyName == nil || [propertyName isEqualToString:@""]) {
359-
return [NSNull class];
360-
}
361-
362-
Class entityClass = NSClassFromString(propertyName);
363-
if (entityClass != nil) {
364-
return entityClass;
365-
}
366-
367-
NSMutableString *singular = [propertyName mutableCopy];
368-
if ([singular.lowercaseString hasSuffix:@"s"]) {
369-
[singular replaceCharactersInRange:NSMakeRange(singular.length - 1, 1) withString:@""];
370-
}
371-
372-
[singular replaceCharactersInRange:NSMakeRange(0, 1) withString:[singular substringToIndex:1].uppercaseString];
373-
374-
entityClass = NSClassFromString(singular);
375-
if (entityClass != nil) {
376-
return entityClass;
377-
}
378-
379-
NSBundle *bundle = [NSBundle bundleForClass:self.class];
380-
if (bundle.bundleDisplayName != nil) {
381-
NSString *moduleName = [NSString stringWithFormat:@"%@.%@", bundle.bundleDisplayName, singular];
382-
entityClass = NSClassFromString(moduleName);
383-
if (entityClass != nil) {
384-
return entityClass;
385-
}
386-
}
387-
388-
if (bundle.bundleName != nil) {
389-
NSString *moduleName = [NSString stringWithFormat:@"%@.%@", bundle.bundleName, singular];
390-
entityClass = NSClassFromString(moduleName);
391-
if (entityClass != nil) {
392-
return entityClass;
393-
}
394-
}
395-
396-
return [NSNull class];
358+
return [NSObject singularizedClassForPropertyName:propertyName];
397359
}
398360

399361
#pragma mark -

CodeQuickKit-ObjC/NSObject+CQKRuntime.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
@param objectClass The Class to inspect for the propertyName.
4242
@return Class for a given the given property with name; Defaults to NSNull.
4343
*/
44-
+ (nonnull Class)classForPropertyName:(nonnull NSString *)propertyName ofClass:(nonnull Class)objectClass;
44+
+ (nonnull Class)classForPropertyName:(nullable NSString *)propertyName ofClass:(nullable Class)objectClass;
4545

4646
/// Returns the last component from NSStringFromClass()
4747
+ (nonnull NSString *)nameForClass:(nonnull Class)objectClass;
@@ -51,4 +51,8 @@
5151

5252
- (BOOL)respondsToSetterForPropertyName:(nullable NSString *)propertyName;
5353

54+
/// Returns the class associated with the property name.
55+
/// Will attempt to append bundle name.
56+
+ (nonnull Class)singularizedClassForPropertyName:(nullable NSString *)propertyName;
57+
5458
@end

CodeQuickKit-ObjC/NSObject+CQKRuntime.m

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
*/
2424

2525
#import "NSObject+CQKRuntime.h"
26+
#import "NSBundle+CQKBundle.h"
2627
#import "CQKSerializableNSObject.h"
2728
#import "CQKLogger.h"
2829
#import <objc/runtime.h>
@@ -58,8 +59,12 @@ @implementation NSObject (CQKRuntime)
5859
return properties;
5960
}
6061

61-
+ (nonnull Class)classForPropertyName:(nonnull NSString *)propertyName ofClass:(nonnull Class)objectClass
62+
+ (Class)classForPropertyName:(NSString *)propertyName ofClass:(Class)objectClass
6263
{
64+
if (propertyName == nil || objectClass == nil) {
65+
return [NSNull class];
66+
}
67+
6368
objc_property_t runtimeProperty = class_getProperty(objectClass, propertyName.UTF8String);
6469
if (runtimeProperty == nil) {
6570
NSString *message = [NSString stringWithFormat:@"Could not determine property name class: class_getProperty failed for property: %@", propertyName];
@@ -132,4 +137,49 @@ - (BOOL)respondsToSetterForPropertyName:(NSString *)propertyName
132137
return [self respondsToSelector:NSSelectorFromString(setter)];
133138
}
134139

140+
+ (Class)singularizedClassForPropertyName:(NSString *)propertyName
141+
{
142+
if (propertyName == nil) {
143+
return [NSNull class];
144+
}
145+
146+
Class entityClass = NSClassFromString(propertyName);
147+
if (entityClass != nil) {
148+
return entityClass;
149+
}
150+
151+
NSMutableString *singular = [propertyName mutableCopy];
152+
if ([singular.lowercaseString hasSuffix:@"s"]) {
153+
[singular replaceCharactersInRange:NSMakeRange(singular.length - 1, 1) withString:@""];
154+
}
155+
156+
[singular replaceCharactersInRange:NSMakeRange(0, 1) withString:[singular substringToIndex:1].uppercaseString];
157+
158+
entityClass = NSClassFromString(singular);
159+
if (entityClass != nil) {
160+
return entityClass;
161+
}
162+
163+
NSBundle *bundle = [NSBundle bundleForClass:self.class];
164+
if (bundle.bundleDisplayName != nil) {
165+
NSString *underscored = [bundle.bundleDisplayName stringByReplacingOccurrencesOfString:@" " withString:@"_"];
166+
NSString *moduleName = [NSString stringWithFormat:@"%@.%@", underscored, singular];
167+
entityClass = NSClassFromString(moduleName);
168+
if (entityClass != nil) {
169+
return entityClass;
170+
}
171+
}
172+
173+
if (bundle.bundleName != nil) {
174+
NSString *underscored = [bundle.bundleName stringByReplacingOccurrencesOfString:@" " withString:@"_"];
175+
NSString *moduleName = [NSString stringWithFormat:@"%@.%@", underscored, singular];
176+
entityClass = NSClassFromString(moduleName);
177+
if (entityClass != nil) {
178+
return entityClass;
179+
}
180+
}
181+
182+
return [NSNull class];
183+
}
184+
135185
@end

0 commit comments

Comments
 (0)