Permalink
Browse files

added TERecordUpdateProtocol

  • Loading branch information...
1 parent 921e84b commit 5c66ca0a7dd5905b75b782ab3a33239d027edc92 @taoeffect committed Jul 16, 2012
Showing with 37 additions and 1 deletion.
  1. +3 −1 TERecord.h
  2. +34 −0 TERecord.m
View
@@ -40,7 +40,9 @@
@protocol TERecord <NSObject, NSCopying, NSCoding>
//@property (nonatomic, strong, readonly) Protocol *protocol;
@property (nonatomic, strong, readonly) NSMutableDictionary *dictCopy;
+@property (nonatomic, strong, readonly) NSMutableDictionary *dict;
@end
// IMPORTANT: YOU *MUST* USE THIS FUNCTION TO CREATE YOUR RECORDS!
-id TERecordCreate(Protocol *proto);
+id TERecordCreate(Protocol *proto);
+void TERecordUpdateProtocol(id<TERecord> r, Protocol *proto);
View
@@ -188,6 +188,7 @@ - (void)setValue:(id)value forKey:(NSString *)key
// as a property of a TERecord supports the NSCoding protocol, *even* if all
// it does in encodeWithCoder is encode a [NSNull null] value. Basically we
// want to make sure that TERecordValues are created by the TERecordCreate function.
+ // If you need to update the record with new properties, call TERecordUpdateProtocol
// if ( !rVal ) {
// rVal = [TERecordValue new];
// [dict setObject:rVal forKey:key];
@@ -255,6 +256,11 @@ - (NSString*)description
return [dict description];
}
+- (NSMutableDictionary*)dict
+{
+ return dict;
+}
+
// many thanks go to this blog post for some of the code here:
// http://blog.lhunath.com/2010/01/clean-up-your-configuration.html
@@ -315,3 +321,31 @@ id TERecordCreate(Protocol *proto)
free(properties);
return [[TERecord alloc] initWithDict:dict];
}
+
+void TERecordUpdateProtocol(id<TERecord> r, Protocol *proto)
+{
+ unsigned int count, i;
+ objc_property_t *properties = protocol_copyPropertyList(proto, &count);
+ NSMutableDictionary *dict = r.dict;
+ for ( i = 0; i < count; ++i ) {
+ NSString *name = [NSString stringWithUTF8String:property_getName(properties[i])];
+ NSString *attrs = [NSString stringWithUTF8String:property_getAttributes(properties[i])];
+ NSArray *attrsAry = [attrs componentsSeparatedByString:@","];
+ BOOL isAtomic = YES;
+ for ( NSString *attr in attrsAry ) {
+ if ( [attr isEqualToString:@"N"] ) {
+ isAtomic = NO;
+ break;
+ }
+ }
+ //log_debug("%s: %@: %@ %@", __func__, NSStringFromProtocol(proto), attrs, name);
+ // now we set defaults so that we can safely handle atomic properties by
+ // creating the hash-tree map fully so that no more node manipulation happens
+ if ( ![dict valueForKey:name] ) {
+ TERecordValue *value = [TERecordValue new];
+ value.atomic = isAtomic;
+ [dict setObject:value forKey:name];
+ }
+ }
+ free(properties);
+}

0 comments on commit 5c66ca0

Please sign in to comment.