189773436a41c7af148000a5834d65aafffa7d6a
[enjoyable.git] / Config.m
1 //
2 // Config.m
3 // Enjoy
4 //
5 // Created by Sam McCall on 4/05/09.
6 //
7
8 #import "Config.h"
9
10 #import "NJInput.h"
11
12 @implementation Config
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 - (Target *)objectForKeyedSubscript:(NJInput *)input {
23 return input ? _entries[input.uid] : nil;
24 }
25
26 - (void)setObject:(Target *)target forKeyedSubscript:(NJInput *)input {
27 if (input) {
28 if (target)
29 _entries[input.uid] = target;
30 else
31 [_entries removeObjectForKey:input.uid];
32 }
33 }
34
35 - (NSDictionary *)serialize {
36 NSMutableDictionary* cfgEntries = [[NSMutableDictionary alloc] initWithCapacity:_entries.count];
37 for (id key in _entries) {
38 id serialized = [_entries[key] serialize];
39 if (serialized)
40 cfgEntries[key] = serialized;
41 }
42 return @{ @"name": _name, @"entries": cfgEntries };
43 }
44
45 @end