adding json helpers, start file renaming

This commit is contained in:
Saul Mora
2011-06-28 00:09:09 -06:00
parent 7db92bcc28
commit ccd003c672
3 changed files with 74 additions and 1 deletions

View File

@@ -20,4 +20,6 @@
#import "NSManagedObjectContext+ActiveRecord.h"
#import "NSPersistentStoreCoordinator+ActiveRecord.h"
#import "NSManagedObjectModel+ActiveRecord.h"
#import "NSPersistentStore+ActiveRecord.h"
#import "NSPersistentStore+ActiveRecord.h"
#import "NSManagedObject+JSONHelpers.h"

View File

@@ -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 <CoreData/CoreData.h>
@interface NSManagedObject (NSManagedObject_JSONHelpers)
- (void) setValuesForKeysWithJSONDictionary:(NSDictionary *)jsonData;
@end

View File

@@ -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