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 // example: 2009-11-04T19:46:20.192723
64 // 2010-01-26T12:07:32-06:00
65 NSMutableDictionary *threadDict = [[NSThread currentThread] threadDictionary];
66 NSDateFormatter *dateFormatter = [threadDict objectForKey:@"iso8601DateFormatter"];
68 dateFormatter = [[NSDateFormatter alloc] init];
69 [dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]];
70 [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSSSSZ"];
71 [threadDict setObject:dateFormatter forKey:@"iso8601DateFormatter"];
72 [dateFormatter release];
75 return [dateFormatter dateFromString:[dateString stringByReplacingOccurrencesOfString:@":"
77 options:NSBackwardsSearch
78 range:NSMakeRange(([dateString length] - 3), 1)]];
81 + (NSDate *)dateFromRFC1123String:(NSString *)dateString {
82 NSMutableDictionary *threadDict = [[NSThread currentThread] threadDictionary];
83 NSDateFormatter *dateFormatter = [threadDict objectForKey:@"rfc1123DateFormatter"];
85 dateFormatter = [[NSDateFormatter alloc] init];
86 [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
87 [dateFormatter setDateFormat:@"eee, dd MMM yyyy HH:mm:ss 'GMT'"];
88 [threadDict setObject:dateFormatter forKey:@"rfc1123DateFormatter"];
89 [dateFormatter release];
91 return [dateFormatter dateFromString:dateString];
94 + (NSString *)localDateDescriptionFromDate:(NSDate *)date {
95 NSMutableDictionary *threadDict = [[NSThread currentThread] threadDictionary];
96 NSDateFormatter *dateFormatterLocal = [threadDict objectForKey:@"dateFormatterLocal"];
97 if (!dateFormatterLocal) {
98 dateFormatterLocal = [[NSDateFormatter alloc] init];
99 [dateFormatterLocal setTimeZone:[NSTimeZone localTimeZone]];
100 [dateFormatterLocal setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
101 [threadDict setObject:dateFormatterLocal forKey:@"dateFormatterLocal"];
102 [dateFormatterLocal release];
104 return [dateFormatterLocal stringFromDate:date];
107 - (NSDate *)dateFromString:(NSString *)dateString {
108 return [[self class] dateFromString:dateString];
111 #pragma mark Comparison
113 - (NSComparisonResult)compare:(ComputeModel *)aComputeModel {
114 return [self.name caseInsensitiveCompare:aComputeModel.name];
117 - (NSString *)description {
118 if ([[self class] respondsToSelector:@selector(toJSON:)]) {
119 return [[self class] performSelector:@selector(toJSON:) withObject:self];
121 return [super description];
126 #pragma mark Memory Management
129 [identifier release];