root / Classes / ComputeModel.m @ 29cc4957
History | View | Annotate | Download (4.2 kB)
1 |
// |
---|---|
2 |
// ComputeModel.m |
3 |
// OpenStack |
4 |
// |
5 |
// Created by Mike Mayo on 10/4/10. |
6 |
// The OpenStack project is provided under the Apache 2.0 license. |
7 |
// |
8 |
|
9 |
#import "ComputeModel.h" |
10 |
#import "NSObject+NSCoding.h" |
11 |
|
12 |
|
13 |
@implementation ComputeModel |
14 |
|
15 |
@synthesize identifier, name; |
16 |
|
17 |
#pragma mark - |
18 |
#pragma mark Serialization |
19 |
|
20 |
- (void)encodeWithCoder: (NSCoder *)coder { |
21 |
[self autoEncodeWithCoder:coder]; |
22 |
} |
23 |
|
24 |
- (id)initWithCoder:(NSCoder *)coder { |
25 |
self = [super init]; |
26 |
if (self) { |
27 |
[self autoDecode:coder]; |
28 |
} |
29 |
return self; |
30 |
} |
31 |
|
32 |
#pragma mark - |
33 |
#pragma mark JSON Parsing |
34 |
|
35 |
- (id)initWithJSONDict:(NSDictionary *)dict { |
36 |
if (self = [super init]) { |
37 |
self.identifier = [[dict objectForKey:@"id"] description]; |
38 |
self.name = [dict objectForKey:@"name"]; |
39 |
} |
40 |
return self; |
41 |
} |
42 |
|
43 |
- (NSInteger)intForKey:(NSString *)key inDict:(NSDictionary *)dict { |
44 |
NSInteger result = 0; |
45 |
if ([dict objectForKey:key] != [NSNull null]) { |
46 |
result = [((NSNumber *)[dict objectForKey:key]) intValue]; |
47 |
} |
48 |
return result; |
49 |
} |
50 |
|
51 |
- (NSDate *)dateForKey:(NSString *)key inDict:(NSDictionary *)dict { |
52 |
NSDate *date = nil; |
53 |
if ([dict objectForKey:key] != [NSNull null]) { |
54 |
date = [self dateFromString:[dict objectForKey:key]]; |
55 |
} |
56 |
return date; |
57 |
} |
58 |
|
59 |
#pragma mark - |
60 |
#pragma mark Date Parser |
61 |
|
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"]; |
67 |
if (!dateFormatter) { |
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]; |
73 |
} |
74 |
|
75 |
return [dateFormatter dateFromString:[dateString stringByReplacingOccurrencesOfString:@":" |
76 |
withString:@"" |
77 |
options:NSBackwardsSearch |
78 |
range:NSMakeRange(([dateString length] - 3), 1)]]; |
79 |
} |
80 |
|
81 |
+ (NSDate *)dateFromRFC1123String:(NSString *)dateString { |
82 |
NSMutableDictionary *threadDict = [[NSThread currentThread] threadDictionary]; |
83 |
NSDateFormatter *dateFormatter = [threadDict objectForKey:@"rfc1123DateFormatter"]; |
84 |
if (!dateFormatter) { |
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]; |
90 |
} |
91 |
return [dateFormatter dateFromString:dateString]; |
92 |
} |
93 |
|
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]; |
103 |
} |
104 |
return [dateFormatterLocal stringFromDate:date]; |
105 |
} |
106 |
|
107 |
- (NSDate *)dateFromString:(NSString *)dateString { |
108 |
return [[self class] dateFromString:dateString]; |
109 |
} |
110 |
|
111 |
#pragma mark Comparison |
112 |
|
113 |
- (NSComparisonResult)compare:(ComputeModel *)aComputeModel { |
114 |
return [self.name caseInsensitiveCompare:aComputeModel.name]; |
115 |
} |
116 |
|
117 |
- (NSString *)description { |
118 |
if ([[self class] respondsToSelector:@selector(toJSON:)]) { |
119 |
return [[self class] performSelector:@selector(toJSON:) withObject:self]; |
120 |
} else { |
121 |
return [super description]; |
122 |
} |
123 |
} |
124 |
|
125 |
#pragma mark - |
126 |
#pragma mark Memory Management |
127 |
|
128 |
- (void)dealloc { |
129 |
[identifier release]; |
130 |
[name release]; |
131 |
[super dealloc]; |
132 |
} |
133 |
|
134 |
@end |