Created
May 10, 2011 20:59
-
-
Save sbrocket/965374 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| + (void)initialize { | |
| if (self == [AppDataBase class]) { | |
| void (^dateAccessorFactory)(NSString*,NSString*) = ^(NSString *key, NSString *strKey) { | |
| void(^setterBlock)(id,NSDate*) = [^void(id self, NSDate *val) { | |
| [self willChangeValueForKey:key]; | |
| [self setPrimitiveValue:val forKey:key]; | |
| [self setPrimitiveValue:nil forKey:strKey]; | |
| [self didChangeValueForKey:key]; | |
| } copy]; | |
| NSString*(^getterBlock)(id) = [^NSString*(id self) { | |
| NSString *strVal = [self primitiveValueForKey:strKey]; | |
| if (strVal == nil) { | |
| strVal = [self stringByFormattingDate:[self valueForKey:key]]; | |
| [self setPrimitiveValue:strVal forKey:strKey]; | |
| } | |
| return strVal; | |
| } copy]; | |
| NSString *setterName = [NSString stringWithFormat:@"set%@%@:", [[key substringToIndex:1] uppercaseString], [key substringFromIndex:1]]; | |
| class_replaceMethod(self, NSSelectorFromString(setterName), pl_imp_implementationWithBlock(setterBlock), "v@:@"); | |
| class_replaceMethod(self, NSSelectorFromString(strKey), pl_imp_implementationWithBlock(getterBlock), "@@:"); | |
| }; | |
| dateAccessorFactory(@"releaseDate", @"releaseDateString"); | |
| dateAccessorFactory(@"createdDate", @"createdDateString"); | |
| dateAccessorFactory(@"updatedDate", @"updatedDateString"); | |
| dateAccessorFactory(@"changeDate", @"changeDateString"); | |
| dateAccessorFactory(@"jsonCreatedDate", @"jsonCreatedString"); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment