Created
December 5, 2011 06:36
-
-
Save zbowling/1432571 to your computer and use it in GitHub Desktop.
SMMangedObject meta
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
//Automagic default implementation | |
- (NSString *)JSONKeyForObjectKeyPath:(NSString *)keyPath { | |
//Anything in the model for an override? | |
NSAttributeDescription *attribute = [self.entity.attributesByName objectForKey:keyPath]; | |
NSString *jsonKey = [[attribute userInfo] objectForKey:@"JSONKey"]; | |
if (jsonKey) | |
return jsonKey; | |
if ([keyPath isEqualToString:@"uuid"]) | |
return @"id"; | |
if ([keyPath isEqualToString:@"dateTime"]) | |
return @"datetime"; | |
NSMutableString *retval = [NSMutableString stringWithCapacity:32]; | |
NSScanner *scanner = [NSScanner scannerWithString:keyPath]; | |
scanner.caseSensitive = YES; | |
NSString *buffer = nil; | |
while ([scanner isAtEnd] == NO) { | |
if ([scanner scanCharactersFromSet:[[NSCharacterSet uppercaseLetterCharacterSet] invertedSet] intoString:&buffer]) { | |
[retval appendString:buffer]; | |
if ([scanner scanCharactersFromSet:[NSCharacterSet uppercaseLetterCharacterSet] intoString:&buffer]) { | |
if ([buffer isEqualToString:@"UUID"]) { | |
[retval appendString:@"_id"]; | |
} | |
else { | |
[retval appendFormat:@"_%@",[buffer lowercaseString]]; | |
} | |
} | |
} | |
} | |
return retval; | |
} | |
//Automagic default implementation | |
- (NSString *) JSONResource { | |
NSString *jsonResource = [self.entity.userInfo objectForKey:@"JSONResource"]; | |
if (!jsonResource) { | |
jsonResource = [NSString stringWithFormat:@"%@",[self class]]; | |
} | |
if ([jsonResource hasPrefix:@"SM"]) | |
{ | |
jsonResource = [jsonResource substringFromIndex:2]; | |
} | |
return jsonResource; | |
} | |
//Automagic default implementation | |
- (NSDictionary *) JSONDictionary { | |
NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithCapacity:[self.entity.properties count]]; | |
[dictionary setObject:[NSString stringWithFormat:@"%@",[self class]] forKey:@"_entity"]; | |
[dictionary setObject:[self JSONResource] forKey:@"_resource"]; | |
for (NSPropertyDescription *property in self.entity.properties) { | |
if ([property isKindOfClass:[NSAttributeDescription class]]) | |
{ | |
NSString *jsonKey = [self JSONKeyForObjectKeyPath:property.name]; | |
if (!jsonKey) | |
continue; | |
id value = [self valueForKey:property.name]; | |
if (!value) { | |
value = [NSNull null]; | |
} | |
if ([value isKindOfClass:[NSDate class]]) { | |
value = [[NSCalendar ISO8601DateFormatter] stringFromDate:value]; | |
} | |
[dictionary setObject:value forKey:jsonKey]; | |
} | |
else if ([property isKindOfClass:[NSRelationshipDescription class]]) | |
{ | |
NSRelationshipDescription *relationship = (NSRelationshipDescription *)property; | |
if (![relationship isToMany]){ | |
NSManagedObject *value = [self valueForKey:property.name]; | |
if (value && [value isKindOfClass:[SMManagedObject class]]) | |
{ | |
SMManagedObject *object = (SMManagedObject *)value; | |
if ([object.entity.propertiesByName objectForKey:@"uuid"]) | |
{ | |
NSString *jsonKey = [[self JSONKeyForObjectKeyPath:property.name] stringByAppendingString:@"_id"]; | |
[dictionary setObject:object.primitiveUuid forKey:jsonKey]; | |
} | |
} | |
} | |
else { | |
NSSet *value; | |
if ([relationship isOrdered]) { | |
value = [(NSOrderedSet *)[self valueForKey:property.name] set]; | |
} | |
else { | |
value = (NSSet*)[self valueForKey:property.name]; | |
} | |
NSString *jsonKey = [self JSONKeyForObjectKeyPath:property.name]; | |
NSMutableArray *values = [NSMutableArray array]; | |
if (value) | |
{ | |
for (SMManagedObject *object in value) { | |
if ([object.entity.propertiesByName objectForKey:@"uuid"]) | |
{ | |
[values addObject:object.primitiveUuid]; | |
} | |
} | |
} | |
[dictionary setObject:values forKey:jsonKey]; | |
} | |
} | |
} | |
return [dictionary copy]; | |
} | |
- (NSDictionary *)JSONChangeSet | |
{ | |
NSMutableDictionary *changeset = [NSMutableDictionary dictionary]; | |
NSDictionary *jsondict = [self JSONDictionary]; | |
[changeset setObject:[jsondict objectForKey:@"_entity"] forKey:@"_entity"]; | |
[changeset setObject:[jsondict objectForKey:@"_resource"] forKey:@"_resource"]; | |
[changeset setObject:[jsondict objectForKey:@"id"] forKey:@"id"]; | |
if(self.isUpdated) { | |
[changeset setObject:@"update" forKey:@"_operation"]; | |
[self.changedValues enumerateKeysAndObjectsUsingBlock:^(NSString *key, id obj, BOOL *stop) { | |
NSString *jsonkey = [self JSONKeyForObjectKeyPath:key]; | |
if (jsonkey) | |
{ | |
id jsonvalue = [jsondict objectForKey:jsonkey]; | |
if (jsonvalue){ | |
[changeset setObject:jsonvalue forKey:jsonkey]; | |
} | |
} | |
}]; | |
} | |
else if (self.isDeleted) { | |
[changeset setObject:@"deleted" forKey:@"_operation"]; | |
} | |
else { | |
[changeset setObject:@"inserted" forKey:@"_operation"]; | |
[changeset addEntriesFromDictionary:jsondict]; | |
} | |
return changeset; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment