From ccd003c67206b69fee330abec289c963480cdf82 Mon Sep 17 00:00:00 2001 From: Saul Mora Date: Tue, 28 Jun 2011 00:09:09 -0600 Subject: [PATCH] adding json helpers, start file renaming --- ...cordFetching.h => CoreData+MagicalRecord.h | 4 +- NSManagedObject+JSONHelpers.h | 15 +++++ NSManagedObject+JSONHelpers.m | 56 +++++++++++++++++++ 3 files changed, 74 insertions(+), 1 deletion(-) rename CoreData+ActiveRecordFetching.h => CoreData+MagicalRecord.h (88%) create mode 100644 NSManagedObject+JSONHelpers.h create mode 100644 NSManagedObject+JSONHelpers.m diff --git a/CoreData+ActiveRecordFetching.h b/CoreData+MagicalRecord.h similarity index 88% rename from CoreData+ActiveRecordFetching.h rename to CoreData+MagicalRecord.h index 15d86cb..64f0569 100644 --- a/CoreData+ActiveRecordFetching.h +++ b/CoreData+MagicalRecord.h @@ -20,4 +20,6 @@ #import "NSManagedObjectContext+ActiveRecord.h" #import "NSPersistentStoreCoordinator+ActiveRecord.h" #import "NSManagedObjectModel+ActiveRecord.h" -#import "NSPersistentStore+ActiveRecord.h" \ No newline at end of file +#import "NSPersistentStore+ActiveRecord.h" + +#import "NSManagedObject+JSONHelpers.h" \ No newline at end of file diff --git a/NSManagedObject+JSONHelpers.h b/NSManagedObject+JSONHelpers.h new file mode 100644 index 0000000..f4aaa4b --- /dev/null +++ b/NSManagedObject+JSONHelpers.h @@ -0,0 +1,15 @@ +// +// NSManagedObject+JSONHelpers.h +// Gathering +// +// Created by Saul Mora on 6/28/11. +// Copyright 2011 Magical Panda Software LLC. All rights reserved. +// + +#import + +@interface NSManagedObject (NSManagedObject_JSONHelpers) + +- (void) setValuesForKeysWithJSONDictionary:(NSDictionary *)jsonData; + +@end diff --git a/NSManagedObject+JSONHelpers.m b/NSManagedObject+JSONHelpers.m new file mode 100644 index 0000000..dfcc143 --- /dev/null +++ b/NSManagedObject+JSONHelpers.m @@ -0,0 +1,56 @@ +// +// NSManagedObject+JSONHelpers.m +// Gathering +// +// Created by Saul Mora on 6/28/11. +// Copyright 2011 Magical Panda Software LLC. All rights reserved. +// + +#import "NSManagedObject+JSONHelpers.h" + + +static NSString * const kNSManagedObjectAttributeJSONKeyMapKey = @"jsonKeyName"; + +@implementation NSString (JSONParsingHelpers) + +- (NSDate *) ga_dateFromJSONString; +{ + NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; + [formatter setDateFormat:@"YYYY-MM-dd'T'HH:mm:ss'Z'"]; + + return [formatter dateFromString:self]; +} + +@end + + + +@implementation NSManagedObject (NSManagedObject_JSONHelpers) + + +- (void) setValuesForKeysWithJSONDictionary:(NSDictionary *)jsonData +{ + NSDictionary *attributes = [[self entity] attributesByName]; + + for (NSString *attributeName in attributes) + { + NSAttributeDescription *attributeInfo = [attributes valueForKey:attributeName]; + + NSString *lookupKey = [[attributeInfo userInfo] valueForKey:kNSManagedObjectAttributeJSONKeyMapKey] ?: attributeName; + id value = [jsonData valueForKey:lookupKey]; + + if (value == nil || [value isEqual:[NSNull null]]) + { + continue; + } + + NSAttributeType attributeType = [attributeInfo attributeType]; + if (attributeType == NSDateAttributeType) + { + value = [value ga_dateFromJSONString]; + } + + [self setValue:value forKey:attributeName]; + } +} +@end