-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathLBModel.m
148 lines (114 loc) · 4.5 KB
/
LBModel.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
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
/**
* @file LBModel.m
*
* @author Michael Schoonmaker
* @copyright (c) 2013 StrongLoop. All rights reserved.
*/
#import <objc/runtime.h>
#import "LBModel.h"
@interface LBModel() {
NSMutableDictionary *__overflow;
}
- (NSMutableDictionary *)_overflow;
@end
@implementation LBModel
- (instancetype)initWithRepository:(SLRepository *)repository parameters:(NSDictionary *)parameters {
self = [super initWithRepository:repository parameters:parameters];
if (self) {
__overflow = [NSMutableDictionary dictionary];
}
return self;
}
- (id)objectForKeyedSubscript:(id <NSCopying>)key {
return [__overflow objectForKey:key];
}
- (void)setObject:(id)obj forKeyedSubscript:(id <NSCopying>)key {
[__overflow setObject:obj forKey:key];
}
- (NSMutableDictionary *)_overflow {
return __overflow;
}
- (NSDictionary *)toDictionary {
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:__overflow];
for (Class targetClass = [self class]; targetClass != [LBModel superclass]; targetClass = [targetClass superclass]) {
unsigned int propertyCount;
objc_property_t *properties = class_copyPropertyList(targetClass, &propertyCount);
for (int i = 0; i < propertyCount; i++) {
NSString *propertyName = [NSString stringWithCString:property_getName(properties[i])
encoding:NSUTF8StringEncoding];
id obj = [self valueForKey:propertyName];
[dict setValue:obj forKey:propertyName];
}
free(properties);
}
return dict;
}
- (NSString *)description {
return [NSString stringWithFormat: @"<%@ %@>", NSStringFromClass([self class]), [self toDictionary]];
}
@end
@implementation LBModelRepository
- (instancetype)initWithClassName:(NSString *)name {
self = [super initWithClassName:name];
if (self) {
NSString *modelClassName = NSStringFromClass([self class]);
const int strlenOfRepository = 10;
modelClassName = [modelClassName substringWithRange:NSMakeRange(0, [modelClassName length] - strlenOfRepository)];
self.modelClass = NSClassFromString(modelClassName);
if (!self.modelClass) {
self.modelClass = [LBModel class];
}
}
return self;
}
- (SLRESTContract *)contract {
SLRESTContract *contract = [SLRESTContract contract];
return contract;
}
- (LBModel *)model {
LBModel *model = (LBModel *)[[self.modelClass alloc] initWithRepository:self parameters:nil];
return model;
}
- (LBModel *)modelWithDictionary:(NSDictionary *)dictionary {
LBModel *model = (LBModel *)[[self.modelClass alloc] initWithRepository:self parameters:dictionary];
[[model _overflow] addEntriesFromDictionary:dictionary];
for (Class targetClass = [model class]; targetClass != [LBModel superclass]; targetClass = [targetClass superclass]) {
unsigned int count;
objc_property_t* props = class_copyPropertyList(targetClass, &count);
for (int i = 0; i < count; i++) {
objc_property_t property = props[i];
const char *name = property_getName(property);
NSString *key = [NSString stringWithUTF8String:name];
id obj = dictionary[key];
if (obj == nil) {
continue;
}
const char *type = property_getAttributes(property);
if ([obj isKindOfClass:[NSString class]]) {
// if the property type is NSDate, convert the string to a date object
if (strncmp(type, "T@\"NSDate\",", 11) == 0) {
obj = [SLObject dateFromEncodedProperty:obj];
}
} else if ([obj isKindOfClass:[NSDictionary class]]) {
// if the property type is NSMutableData, convert the json object to a data object
if (strncmp(type, "T@\"NSMutableData\",", 18) == 0 ||
strncmp(type, "T@\"NSData\",", 11) == 0) {
obj = [SLObject dataFromEncodedProperty:obj];
}
// if the property type is CLLocation, convert the json object to a location object
else if (strncmp(type, "T@\"CLLocation\",", 15) == 0) {
obj = [SLObject locationFromEncodedProperty:obj];
}
}
@try {
[model setValue:obj forKey:key];
}
@catch (NSException *e) {
// ignore any failure
}
}
free(props);
}
return model;
}
@end