Created
February 6, 2013 23:22
-
-
Save bdittmer/4726819 to your computer and use it in GitHub Desktop.
A better way to deal with mapping dictionaries -> model objects
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NSString *giIdKey = @"id"; | |
NSString *giJobIdKey = @"job_id"; | |
NSString *giPercentCompleteKey = @"percent_complete"; | |
NSString *giPercentReservedKey = @"percent_reserved"; | |
NSString *giGoalsKey = @"goals"; | |
NSString *giRewardsKey = @"rewards"; | |
@interface ESGroupIncentive() | |
+ (NSDictionary *)valueMap; | |
@end | |
typedef id (^ValueConverter)(id obj); | |
@implementation ESGroupIncentive | |
+ (ESGroupIncentive *)fromDictionary:(NSDictionary *)d { | |
ESGroupIncentive *gi = [[ESGroupIncentive alloc] init]; | |
NSDictionary *valueMap = [self valueMap]; | |
NSDictionary *valueConverters = [self valueConverters]; | |
for (NSString *key in valueMap) { | |
if (d[key]) { | |
if (valueConverters[key]) { | |
ValueConverter valueConverter = valueConverters[key]; | |
id value = valueConverter(d[key]); | |
[gi setValue:value forKey:valueMap[key]]; | |
} else { | |
[gi setValue:d[key] forKey:valueMap[key]]; | |
} | |
} | |
} | |
return [gi autorelease]; | |
} | |
- (id)copyWithZone:(NSZone *)zone { | |
ESGroupIncentive *gi = [[ESGroupIncentive allocWithZone:zone] init]; | |
NSArray *values = [[ESGroupIncentive valueMap] allValues]; | |
for (NSString *value in values) { | |
[gi setValue:[[[self valueForKey:value] copy] autorelease] forKey:value]; | |
} | |
return gi; | |
} | |
- (void)encodeWithCoder:(NSCoder *)aCoder { | |
NSDictionary *valueMap = [ESGroupIncentive valueMap]; | |
for (NSString *key in valueMap) { | |
[aCoder encodeObject:[self valueForKey:valueMap[key]] forKey:key]; | |
} | |
} | |
- (id)initWithCoder:(NSCoder *)aDecoder { | |
self = [super init]; | |
NSDictionary *valueMap = [ESGroupIncentive valueMap]; | |
for (NSString *key in valueMap) { | |
[self setValue:[aDecoder decodeObjectForKey:key] forKey:valueMap[key]]; | |
} | |
return self; | |
} | |
+ (NSDictionary *)valueMap { | |
return @{giIdKey:@"_groupIncentiveId", giJobIdKey:@"_jobId", giPercentCompleteKey:@"_percentComplete", | |
giPercentReservedKey:@"_percentReserved", giGoalsKey:@"_goals", giRewardsKey:@"_rewards", @"date":@"_date"}; | |
} | |
+ (NSDictionary *)valueConverters { | |
return @{@"date": Block_copy(^id (id obj) { | |
NSString *value = (NSString *)obj; | |
NSDateFormatter *formatter = [ESUtility utcDateFormatter]; | |
return [formatter dateFromString:value]; | |
})}; | |
} | |
- (void)dealloc { | |
[_groupIncentiveId release]; | |
[_jobId release]; | |
[_percentComplete release]; | |
[_percentReserved release]; | |
[_goals release]; | |
[_rewards release]; | |
[super dealloc]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment