Skip to content

Instantly share code, notes, and snippets.

@stevenhuey
Created October 25, 2012 14:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stevenhuey/3953082 to your computer and use it in GitHub Desktop.
Save stevenhuey/3953082 to your computer and use it in GitHub Desktop.
Before Mantle
@implementation CBCosmDatastream
- (id)initWithAttributes:(NSDictionary*)attributes
{
self = [super init];
if (self)
{
if (!attributes)
{
return nil;
}
for (NSString* key in attributes)
{
[self setValue:[attributes valueForKey:key]
forKey:key];
}
}
return self;
}
#pragma mark - Equality
- (NSUInteger)hash
{
return [self.datastreamId hash];
}
- (BOOL)isEqual:(id)object
{
if ([object isKindOfClass:[CBCosmDatastream class]])
{
CBCosmDatastream* otherDatastream = (CBCosmDatastream*)object;
return [self.datastreamId isEqualToNumber:otherDatastream.datastreamId];
}
else
{
return NO;
}
}
#pragma mark - KVC
- (void)setValue:(id)value forKey:(NSString*)key
{
if ([key isEqualToString:@"id"])
{
[super setValue:value forKey:@"datastreamId"];
}
else if ([key isEqualToString:@"at"])
{
[super setValue:value forKey:@"updated"];
}
else if ([key isEqualToString:@"current_value"])
{
[super setValue:value forKey:@"value"];
}
else if ([key isEqualToString:@"min_value"])
{
[super setValue:value forKey:@"minValue"];
}
else if ([key isEqualToString:@"max_value"])
{
[super setValue:value forKey:@"maxValue"];
}
else if ([key isEqualToString:@"datapoints"])
{
if ([value isKindOfClass:[NSArray class]])
{
NSArray* values = (NSArray*)value;
NSMutableArray* datapoints = [NSMutableArray arrayWithCapacity:[values count]];
for (NSDictionary* attributes in values)
{
CBCosmDatapoint* datapoint = [[CBCosmDatapoint alloc] initWithAttributes:attributes];
[datapoints addObject:datapoint];
}
_datapoints = datapoints;
}
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment