dchohfi/KeyValueObjectMapping — pull request #27 by akinsella (2013-05-11 13:29:03+00:00)
Title: Adding support for current object context in DCCustomParsers

Hi,

In custom parser we don't have any information about the current object (dictionary) being processed. I modified master from last commit to add the current object context (the dictionary) in the DCCustomParserBlock.

By this add, you can leverage other params from the same object (dictionary) to decide how you have to parse the current attribute being processed.

Some example:

DCParserConfiguration *config = [[DCParserConfiguration alloc] init];

...

DCCustomParserBlock valueParserBlock = ^id(__weak NSDictionary *dictionary, NSString *__weak attributeName, __weak Class destinationClass, __weak id value) {
id type = dictionary[@"type"];
if ([type isEqualToString:@"brand"]) {
return [XBMapper parseObject:value intoObjectsOfType:LCBrand.class];
}
else if ([@[@"category", @"option"] containsObject: type]) {
NSMutableIndexSet *indexSet = [NSMutableIndexSet indexSet];
for (NSNumber *item in value) {
[indexSet addIndex:(NSUInteger)item.integerValue];
}
return indexSet;
}
else {
return value;
}
};

DCCustomParser *valueParser = [[DCCustomParser alloc] initWithBlockParser:valueParserBlock
forAttributeName:@"_value"
onDestinationClass:[self class]];
[config addCustomParsersObject:valueParser];

return config;