5 // Created by Mike Mayo on 10/4/10.
6 // The OpenStack project is provided under the Apache 2.0 license.
9 #import "ComputeModel.h"
10 #import "NSObject+NSCoding.h"
13 @implementation ComputeModel
15 @synthesize identifier, name;
18 #pragma mark Serialization
20 - (void)encodeWithCoder: (NSCoder *)coder {
21 [self autoEncodeWithCoder:coder];
24 - (id)initWithCoder:(NSCoder *)coder {
27 [self autoDecode:coder];
33 #pragma mark JSON Parsing
35 - (id)initWithJSONDict:(NSDictionary *)dict {
36 if (self = [super init]) {
37 self.identifier = [[dict objectForKey:@"id"] description];
38 self.name = [dict objectForKey:@"name"];
43 - (NSInteger)intForKey:(NSString *)key inDict:(NSDictionary *)dict {
45 if ([dict objectForKey:key] != [NSNull null]) {
46 result = [((NSNumber *)[dict objectForKey:key]) intValue];
51 - (NSDate *)dateForKey:(NSString *)key inDict:(NSDictionary *)dict {
53 if ([dict objectForKey:key] != [NSNull null]) {
54 date = [self dateFromString:[dict objectForKey:key]];
60 #pragma mark Date Parser
62 + (NSDate *)dateFromString:(NSString *)dateString {
63 return nil; // temporarily removing date parsing for performance
66 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
67 [dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]];
68 // example: 2009-11-04T19:46:20.192723
69 // 2010-01-26T12:07:32-06:00
71 // this is nasty, but -06:00 is not a valid timezone format. converting to -0600 style
72 dateString = [NSString stringWithFormat:@"%@%@", [dateString substringToIndex:22], [dateString substringFromIndex:23]];
73 [dateFormatter setDateFormat:@"yyyy-MM-dd'T'H:mm:ssZ"];
75 NSDate *date = [dateFormatter dateFromString:dateString];
76 [dateFormatter release];
82 - (NSDate *)dateFromString:(NSString *)dateString {
83 return [[self class] dateFromString:dateString];
86 #pragma mark Comparison
88 - (NSComparisonResult)compare:(ComputeModel *)aComputeModel {
89 return [self.name caseInsensitiveCompare:aComputeModel.name];
92 - (NSString *)description {
93 if ([[self class] respondsToSelector:@selector(toJSON:)]) {
94 return [[self class] performSelector:@selector(toJSON:) withObject:self];
96 return [super description];
101 #pragma mark Memory Management
104 [identifier release];