Created
August 5, 2013 20:09
-
-
Save Pretz/6159132 to your computer and use it in GitHub Desktop.
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
+ (NSDateFormatter *)dateFormatter { | |
return [[ISO8601DateFormatter alloc] init]; | |
} | |
+ (NSValueTransformer *)jsonDateValueTransformer { | |
return [MTLValueTransformer reversibleTransformerWithForwardBlock:^(NSString *str) { | |
return [self.dateFormatter dateFromString:str]; | |
} reverseBlock:^(NSDate *date) { | |
return [self.dateFormatter stringFromDate:date]; | |
}]; | |
} | |
+ (NSValueTransformer *)urlValueTransformer { | |
return [MTLValueTransformer reversibleTransformerWithForwardBlock:^NSURL *(NSString *url) { | |
return [NSURL URLWithString:url]; | |
} reverseBlock:^NSString *(NSURL *url) { | |
return [url absoluteString]; | |
}]; | |
} | |
/** Returns a rails-compatible transformer if the property has a class of NSDate or NSURL */ | |
+ (NSValueTransformer *)JSONTransformerForKey:(NSString *)key { | |
objc_property_t property = class_getProperty([self class], [key cStringUsingEncoding:NSUTF8StringEncoding]); | |
mtl_propertyAttributes *attributes = mtl_copyPropertyAttributes(property); | |
@try { | |
if ([attributes->objectClass isSubclassOfClass:[NSDate class]]) { | |
return [self jsonDateValueTransformer]; | |
} else if ([attributes->objectClass isSubclassOfClass:[NSURL class]]) { | |
return [self urlValueTransformer]; | |
} else { | |
return nil; | |
} | |
} @finally { | |
if (attributes) free(attributes); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment