/
ANResource.m
91 lines (68 loc) · 2.24 KB
/
ANResource.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//
// ANRepresentation.m
// AppNetKit
//
// Created by Brent Royal-Gordon on 8/18/12.
// Copyright (c) 2012 Architechies. See README.md for licensing information.
//
#import "ANResource.h"
#import "ISO8601DateFormatter.h"
#import "ANSession.h"
#import "ANSession+ANResource_Private.h"
#import <objc/runtime.h>
NSString * const ANResourceDidUpdateNotification = @"ANResourceDidUpdate";
@implementation ANResource
+ (ISO8601DateFormatter *)dateFormatter {
static ISO8601DateFormatter * singleton;
static dispatch_once_t once;
dispatch_once(&once, ^{
singleton = [ISO8601DateFormatter new];
});
return singleton;
}
+ (NSNumberFormatter *)IDFormatter {
static NSNumberFormatter * singleton;
static dispatch_once_t once;
dispatch_once(&once, ^{
singleton = [NSNumberFormatter new];
});
return singleton;
}
- (id)initWithRepresentation:(NSDictionary *)rep session:(ANSession *)session {
NSParameterAssert(session);
if(!rep) {
self = nil;
}
if((self = [super init])) {
_session = session;
_representation = rep.copy;
}
return self;
}
- (void)setRepresentation:(NSDictionary *)representation {
if([representation isEqualToDictionary:_representation]) {
return;
}
[self willChangeValueForKey:@"representation"];
_representation = representation;
objc_removeAssociatedObjects(self);
[self didChangeValueForKey:@"representation"];
NSNotification * note = [NSNotification notificationWithName:ANResourceDidUpdateNotification object:self];
[NSNotificationQueue.defaultQueue enqueueNotification:note postingStyle:NSPostASAP coalesceMask:NSNotificationCoalescingOnName|NSNotificationCoalescingOnSender forModes:nil];
}
+ (BOOL)automaticallyNotifiesObserversForKey:(NSString *)key {
if([key isEqualToString:@"representation"]) {
return NO;
}
return [super automaticallyNotifiesObserversForKey:key];
}
- (NSUInteger)hash {
return (NSUInteger)self.representation.hash ^ (NSUInteger)self.class;
}
- (BOOL)isEqual:(ANResource*)object {
if(self.class != object.class) {
return NO;
}
return [self.representation isEqual:object.representation];
}
@end