mirror of
https://github.com/zhigang1992/RestKit.git
synced 2026-04-02 09:31:32 +08:00
* Introduce RKObjectMappable#relationshipsToSerialize to define nested relationships to post * New helper methods for working with relationship serializations. * Extended Rails router to serialize nested objects according to Rails idioms
55 lines
2.4 KiB
Objective-C
55 lines
2.4 KiB
Objective-C
//
|
|
// RKObjectMappable.m
|
|
// RestKit
|
|
//
|
|
// Created by Blake Watters on 1/20/11.
|
|
// Copyright 2011 Two Toasters. All rights reserved.
|
|
//
|
|
|
|
#import "RKObjectMappable.h"
|
|
|
|
// Return all the mapped properties of object in a dictionary
|
|
NSDictionary* RKObjectMappableGetProperties(NSObject<RKObjectMappable>*object) {
|
|
NSDictionary* mappings = [[object class] elementToPropertyMappings];
|
|
NSMutableDictionary* propertyNamesAndValues = [NSMutableDictionary dictionaryWithCapacity:[mappings count]];
|
|
// Return all the properties of this model in a dictionary under their element names
|
|
for (NSString* elementName in mappings) {
|
|
NSString* propertyName = [mappings valueForKey:elementName];
|
|
id propertyValue = [object valueForKey:propertyName];
|
|
[propertyNamesAndValues setValue:propertyValue forKey:propertyName];
|
|
}
|
|
|
|
return [NSDictionary dictionaryWithDictionary:propertyNamesAndValues];
|
|
}
|
|
|
|
// Return all the mapped properties of object in a dictionary under their element names
|
|
NSDictionary* RKObjectMappableGetPropertiesByElement(NSObject<RKObjectMappable>*object) {
|
|
NSDictionary* mappings = [[object class] elementToPropertyMappings];
|
|
NSMutableDictionary* elementsAndPropertyValues = [NSMutableDictionary dictionaryWithCapacity:[mappings count]];
|
|
|
|
for (NSString* elementName in mappings) {
|
|
NSString* propertyName = [mappings valueForKey:elementName];
|
|
id propertyValue = [object valueForKey:propertyName];
|
|
[elementsAndPropertyValues setValue:propertyValue forKey:elementName];
|
|
}
|
|
|
|
return [NSDictionary dictionaryWithDictionary:elementsAndPropertyValues];
|
|
}
|
|
|
|
// Return all the serialzable mapped relationships of object in a dictionary under their element names
|
|
NSDictionary* RKObjectMappableGetRelationshipsByElement(NSObject<RKObjectMappable>*object)
|
|
{
|
|
NSArray* relationshipsToSerialize = [[object class] relationshipsToSerialize];
|
|
NSDictionary* mappings = [[object class] elementToRelationshipMappings];
|
|
NSMutableDictionary* elementsAndRelationships = [NSMutableDictionary dictionaryWithCapacity:[relationshipsToSerialize count]];
|
|
for (NSString* elementName in mappings) {
|
|
NSString* propertyName = [mappings valueForKey:elementName];
|
|
if ([relationshipsToSerialize containsObject:propertyName]) {
|
|
id propertyValue = [object valueForKey:propertyName];
|
|
[elementsAndRelationships setValue:propertyValue forKey:elementName];
|
|
}
|
|
}
|
|
|
|
return [NSDictionary dictionaryWithDictionary:elementsAndRelationships];
|
|
}
|