Use represented objects rather than index hackery. Remove the mapping name from the...
[enjoyable.git] / NJMapping.m
1 //
2 // NJMapping.m
3 // Enjoy
4 //
5 // Created by Sam McCall on 4/05/09.
6 //
7
8 #import "NJMapping.h"
9
10 #import "NJInput.h"
11
12 @implementation NJMapping
13
14 - (id)initWithName:(NSString *)name {
15 if ((self = [super init])) {
16 self.name = name ? name : @"Untitled";
17 _entries = [[NSMutableDictionary alloc] init];
18 }
19 return self;
20 }
21
22 - (NJOutput *)objectForKeyedSubscript:(NJInput *)input {
23 return input ? _entries[input.uid] : nil;
24 }
25
26 - (void)setObject:(NJOutput *)output forKeyedSubscript:(NJInput *)input {
27 if (input) {
28 if (output)
29 _entries[input.uid] = output;
30 else
31 [_entries removeObjectForKey:input.uid];
32 }
33 }
34
35 - (NSDictionary *)serialize {
36 NSMutableDictionary *entries = [[NSMutableDictionary alloc] initWithCapacity:_entries.count];
37 for (id key in _entries) {
38 id serialized = [_entries[key] serialize];
39 if (serialized)
40 entries[key] = serialized;
41 }
42 return @{ @"name": _name, @"entries": entries };
43 }
44
45 @end