-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAMProxyObjectCommiter.m
227 lines (189 loc) · 6.58 KB
/
AMProxyObjectCommiter.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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
//
// AMProxyObjectCommiter.m
// Created by Joan Martin.
// Take a look to my repos at http://github.com/vilanovi
//
// Copyright (c) 2013 Joan Martin, [email protected].
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE
#import "AMProxyObjectCommiter.h"
NSString * const AMProxyObjectCommiterNillyfiedValuesKey = @"AMProxyObjectCommiterNillyfiedValuesKey";
NSString * const AMProxyObjectCommiterChangedValuesKey = @"AMProxyObjectCommiterChangedValuesKey";
@implementation AMProxyObjectCommiter
{
id _object;
NSMutableDictionary *_keyedValues;
NSMutableSet *_nilValues;
NSMutableSet *_trackedGetSelectors;
NSMutableSet *_trackedSetSelectors;
NSMutableDictionary *_trackedSelectorsMap;
}
- (id)__initWithObject:(id)object
{
if (self)
{
_object = object;
_keyedValues = [NSMutableDictionary dictionary];
_nilValues = [NSMutableSet set];
_trackedSetSelectors = [NSMutableSet set];
_trackedGetSelectors = [NSMutableSet set];
_trackedSelectorsMap = [NSMutableDictionary dictionary];
}
return self;
}
#pragma mark Properties
- (id)__object
{
return _object;
}
#pragma mark NSProxy Methods
- (NSMethodSignature *)methodSignatureForSelector:(SEL)sel
{
return [_object methodSignatureForSelector:sel];
}
- (void)forwardInvocation:(NSInvocation *)invocation
{
if (invocation.methodSignature == [_object methodSignatureForSelector:@selector(setValue:forKey:)])
{
__unsafe_unretained id tempValue = nil;
NSString *key = nil;
[invocation getArgument:&tempValue atIndex:2];
[invocation getArgument:&key atIndex:3];
id value = tempValue;
if (key != nil)
{
id oldValue = [_object valueForKey:key];
if (![oldValue isEqual:value])
{
if (value != nil)
{
[_keyedValues setValue:value forKey:key];
[_nilValues removeObject:key];
}
else
{
[_nilValues addObject:key];
}
}
else
{
[_keyedValues removeObjectForKey:key];
[_nilValues removeObject:key];
}
[invocation setTarget:nil];
[invocation invoke];
}
else
{
[invocation setTarget:_object];
[invocation invoke];
}
}
else if (invocation.methodSignature == [_object methodSignatureForSelector:@selector(valueForKey:)])
{
NSString *key = nil;
[invocation getArgument:&key atIndex:2];
if ([_keyedValues valueForKey:key])
{
[invocation setTarget:_keyedValues];
[invocation invoke];
}
else
{
[invocation setTarget:_object];
[invocation invoke];
}
}
else if ([_trackedGetSelectors containsObject:[NSValue valueWithPointer:invocation.selector]])
{
[invocation setTarget:_keyedValues];
NSValue *value = [NSValue valueWithPointer:invocation.selector];
[invocation setArgument:&value atIndex:2];
[invocation invoke];
}
else if ([_trackedSetSelectors containsObject:[NSValue valueWithPointer:invocation.selector]])
{
__unsafe_unretained id tempValue = nil;
[invocation getArgument:&tempValue atIndex:2];
id value = tempValue;
[_keyedValues setValue:value forKey:[_trackedSelectorsMap objectForKey:[NSValue valueWithPointer:invocation.selector]]];
[invocation setTarget:nil];
[invocation invoke];
}
else
{
[invocation setTarget:_object];
[invocation invoke];
}
}
- (BOOL)respondsToSelector:(SEL)aSelector
{
BOOL responds = [super respondsToSelector:aSelector];
if (!responds)
responds = [_object respondsToSelector:aSelector];
return responds;
}
#pragma mark Public Methods
- (void)__commitChanges
{
NSDictionary *keyValues = [_keyedValues copy];
[_object setValuesForKeysWithDictionary:keyValues];
for (NSString *key in _nilValues)
[_object setValue:nil forKey:key];
[_keyedValues removeAllObjects];
[_nilValues removeAllObjects];
}
- (void)__commitChangesForKey:(NSString*)key
{
id value = [_keyedValues valueForKey:key];
if (value)
[_object setValue:value forKey:key];
else if ([_nilValues containsObject:key])
[_object setValue:nil forKey:key];
[_keyedValues removeObjectForKey:key];
[_nilValues removeObject:key];
}
- (NSDictionary*)__changes
{
return @{AMProxyObjectCommiterChangedValuesKey : [_keyedValues copy],
AMProxyObjectCommiterNillyfiedValuesKey : [_nilValues copy]};
}
- (BOOL)__didChangeForKey:(NSString*)key
{
return [_keyedValues valueForKey:key] != nil || [_nilValues containsObject:key];
}
- (id)__editedValueForKey:(NSString*)key
{
id value = [_keyedValues valueForKey:key];
if (value)
return value;
if ([_nilValues containsObject:key])
return [NSNull null];
return nil;
}
- (id)__originalValueForKey:(NSString*)key
{
return [_object valueForKey:key];
}
- (void)__trackSetSelector:(SEL)setSelector forGetSelector:(SEL)getSelector
{
[_trackedSetSelectors addObject:[NSValue valueWithPointer:setSelector]];
[_trackedGetSelectors addObject:[NSValue valueWithPointer:getSelector]];
[_trackedSelectorsMap setObject:[NSValue valueWithPointer:getSelector] forKey:[NSValue valueWithPointer:setSelector]];
}
@end