-
Notifications
You must be signed in to change notification settings - Fork 0
/
SWXMLClassMapping.m
61 lines (44 loc) · 1.72 KB
/
SWXMLClassMapping.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//
// SWXMLClassMapping.m
// This file is part of the "SWXMLMapping" project, and is distributed under the MIT License.
//
// Created by Samuel Williams on 13/11/05.
// Copyright 2005 Samuel Williams. All rights reserved.
//
#import "SWXMLClassMapping.h"
#import "SWXMLMemberMapping.h"
@implementation SWXMLClassMapping
@synthesize objectClassName = _objectClassName, tag = _tag, members = _members, attributes = _attributes;
- (NSArray *)membersForObject:(id)object withMapping:(SWXMLMapping *)mapping {
NSMutableArray * children = [NSMutableArray new];
for (SWXMLMemberMapping * memberMapping in self.members) {
NSString * child = [memberMapping serializedObjectMember:object withMapping:mapping];
if (child && child.length > 0)
[children addObject:child];
}
return children;
}
- (NSString *)serializeObject:(id)object withMapping:(SWXMLMapping*)mapping {
NSArray * children = [self membersForObject:object withMapping:mapping];
NSDictionary * attributes = nil;
if ((self.attributes)[@"id"]) {
id objectIdentificationKeyPath = (self.attributes)[@"id"];
// Wee shortcut
if ([objectIdentificationKeyPath isEqualTo:@"managed-object"]) {
objectIdentificationKeyPath = @"objectID.URIRepresentation";
}
id objectIdentification = [object valueForKeyPath:objectIdentificationKeyPath];
attributes = @{@"id": objectIdentification};
}
return [SWXMLTags tagNamed:self.tag forValue:[children componentsJoinedByString:@"\n"] withAttributes:attributes];
}
- (instancetype) initWithTag:(NSString*)tag forClass:(NSString*)className attributes:(NSDictionary *)attributes {
self = [super init];
if (self) {
self.tag = tag;
self.objectClassName = className;
self.attributes = attributes;
}
return self;
}
@end