-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGMModel.m
More file actions
196 lines (148 loc) · 5.42 KB
/
GMModel.m
File metadata and controls
196 lines (148 loc) · 5.42 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
//
// GMModel.m
//
// Created by Gersham Meharg on 11-12-08.
// Copyright (c) 2011 Gersham Meharg. All rights reserved.
//
#import "GMModel.h"
@implementation GMModel
@synthesize values = _values;
@synthesize uuid = _uuid;
static NSMutableDictionary *camelCaseStrings = nil;
+ (NSString *)camelCaseString:(NSString *)string {
if (camelCaseStrings == nil)
camelCaseStrings = [NSMutableDictionary dictionary];
if ([camelCaseStrings objectForKey:string])
return [camelCaseStrings objectForKey:string];
NSMutableString *output = [NSMutableString string];
BOOL makeNextCharacterUpperCase = NO;
for (NSInteger idx = 0; idx < [string length]; idx += 1) {
unichar c = [string characterAtIndex:idx];
if (c == '_') {
makeNextCharacterUpperCase = YES;
} else if (makeNextCharacterUpperCase) {
[output appendString:[[NSString stringWithCharacters:&c length:1] uppercaseString]];
makeNextCharacterUpperCase = NO;
} else {
[output appendFormat:@"%C", c];
}
}
[camelCaseStrings setObject:output forKey:string];
return output;
}
+ (GMModel *)objectWithValues:(NSDictionary *)values createIfNotFound:(BOOL)create {
return nil;
}
+ (NSArray *)objectArrayFromArray:(NSArray *)array {
return nil;
}
+ (NSString *)uuidFromValues:(NSDictionary *)values {
if ([values valueForKey:@"id"]) {
return [values valueForKey:@"id"];
} else if ([values valueForKey:@"uuid"]) {
return [values valueForKey:@"uuid"];
} else {
return nil;
}
}
- (void)setValues:(NSMutableDictionary *)values {
if (_values == nil)
_values = [NSMutableDictionary dictionaryWithCapacity:values.count];
if (values == nil)
return;
for (NSString *key in values.allKeys) {
[self setValue:[values valueForKey:key] forKey:key];
}
}
- (NSMutableDictionary *)values {
if (_values == nil)
_values = [NSMutableDictionary dictionary];
return _values;
}
- (void)updateWithValues:(NSDictionary *)values {
if (_values == nil)
_values = [NSMutableDictionary dictionaryWithCapacity:values.count];
if (values == nil)
return;
for (NSString *key in values.allKeys) {
[self setValue:[values valueForKey:key] forKey:key];
}
}
- (void)setValue:(id)value forKey:(NSString *)key {
NSString *camelKey = [GMModel camelCaseString:key];
if (_values == nil)
_values = [NSMutableDictionary dictionary];
if (value == nil || [value isEqual:[NSNull null]]) {
if ([_values objectForKey:camelKey]) {
[_values removeObjectForKey:camelKey];
}
} else if ([camelKey isEqualToString:@"id"]) {
self.uuid = value;
} else if ([camelKey isEqualToString:@"uuid"]) {
self.uuid = value;
} else if ([value isKindOfClass:[NSArray class]]) {
[_values setValue:[self parseValuesFromArray:value] forKey:camelKey];
} else {
[_values setValue:value forKey:camelKey];
}
}
- (NSArray *)parseValuesFromArray:(NSArray *)array {
NSMutableArray *result = [NSMutableArray array];
for (id item in array) {
if ([item isKindOfClass:[NSDictionary dictionary]]) {
NSDictionary *dictionary = [NSMutableDictionary dictionary];
for (NSString *key in [(NSDictionary *)item allKeys]) {
id value = [(NSDictionary *)item valueForKey:key];
if (value == nil || [value isEqual:[NSNull null]])
continue;
if ([value isKindOfClass:[NSArray class]]) {
[dictionary setValue:[self parseValuesFromArray:value] forKey:key];
} else {
[dictionary setValue:value forKey:key];
}
}
[result addObject:dictionary];
} else {
[result addObject:item];
}
}
return result;
}
- (id)valueForKey:(NSString *)key {
return [_values valueForKey:key];
}
- (id)valueForKeyPath:(NSString *)keyPath {
return [_values valueForKeyPath:keyPath];
}
- (NSString *)description {
return [NSString stringWithFormat:@"GMModel %@", self.uuid];
}
# pragma mark - Dynamic Properties
- (id)dictionaryValueForKey:(NSString *) key {
return [self.values objectForKey:key];
}
static NSString *accessorGetter(id self, SEL _cmd) {
return [self dictionaryValueForKey:NSStringFromSelector(_cmd)];
}
- (void)setDictionaryValue:(id)value forKey:(NSString *)key {
[self.values setValue:value forKey:key];
}
static void accessorSetter(id self, SEL _cmd, id newValue)
{
NSString *method = NSStringFromSelector(_cmd);
NSString *anID = [[[method stringByReplacingCharactersInRange:NSMakeRange(0, 3) withString:@""] lowercaseString] stringByReplacingOccurrencesOfString:@":" withString:@""];
[self setDictionaryValue:newValue forKey:anID];
}
+ (BOOL)resolveInstanceMethod:(SEL)aSEL
{
NSString *method = NSStringFromSelector(aSEL);
if ([method hasPrefix:@"set"]) {
class_addMethod([self class], aSEL, (IMP) accessorSetter, "v@:@");
return YES;
} else {
class_addMethod([self class], aSEL, (IMP) accessorGetter, "@@:");
return YES;
}
return [super resolveInstanceMethod:aSEL];
}
@end