Permalink
Browse files

Make the attributes truly immutable

  • Loading branch information...
1 parent 32c4f80 commit 410888ca805de6ed7879cc1e806d9e35b8de0da7 @danielctull committed Jan 3, 2013
Showing with 47 additions and 22 deletions.
  1. +7 −5 DCTImageCache/DCTImageCacheAttributes.h
  2. +40 −17 DCTImageCache/DCTImageCacheAttributes.m
View
12 DCTImageCache/DCTImageCacheAttributes.h
@@ -8,14 +8,16 @@
#import <UIKit/UIKit.h>
+extern NSString *const DCTImageCacheAttributesKey;
+extern NSString *const DCTImageCacheAttributesSize;
+extern NSString *const DCTImageCacheAttributesCreatedBefore;
+
/**
*/
@interface DCTImageCacheAttributes : NSObject
-@property (nonatomic, copy) NSString *key;
-@property (nonatomic, assign) CGSize size;
-@property (nonatomic, copy) NSDate *createdBefore;
-
-- (NSString *)identifier;
+- (id)initWithDictionary:(NSDictionary *)dictionary;
+@property (nonatomic, readonly, copy) NSDictionary *dictionary;
+@property (nonatomic, readonly) NSString *identifier;
@end
View
57 DCTImageCache/DCTImageCacheAttributes.m
@@ -8,26 +8,32 @@
#import "_DCTImageCacheAttributes.h"
+NSString *const DCTImageCacheAttributesKey = @"DCTImageCacheAttributesKey";
+NSString *const DCTImageCacheAttributesSize = @"DCTImageCacheAttributesSize";
+NSString *const DCTImageCacheAttributesCreatedBefore = @"DCTImageCacheAttributesCreatedBefore";
+
CGSize const DCTImageCacheAttributesNullSize = {-CGFLOAT_MAX, -CGFLOAT_MAX};
-@implementation DCTImageCacheAttributes {
- NSString *_identifier;
-}
+@implementation DCTImageCacheAttributes
-- (id)init {
- self = [super init];
+- (id)initWithDictionary:(NSDictionary *)dictionary {
+ self = [self init];
if (!self) return nil;
- _size = DCTImageCacheAttributesNullSize;
+ _dictionary = [dictionary copy];
return self;
}
- (NSString *)identifier {
- if (!_identifier) _identifier = [NSString stringWithFormat:@"key:%@.size:%@.createdBefore:%@", self.key, NSStringFromCGSize(self.size), @([self.createdBefore timeIntervalSinceReferenceDate])];
- return _identifier;
+ return [self.dictionary description];
}
- (NSString *)description {
- return [NSString stringWithFormat:@"<%@: %p; key = %@; size = %@; createdBefore = %@>", NSStringFromClass([self class]), self, self.key, NSStringFromCGSize(self.size), self.createdBefore];
+ return [NSString stringWithFormat:@"<%@: %p; key = %@; size = %@; createdBefore = %@>",
+ NSStringFromClass([self class]),
+ self,
+ [self _key],
+ [self _sizeString],
+ [self _createdBefore]];
}
- (NSFetchRequest *)_fetchRequest {
@@ -36,18 +42,21 @@ - (NSFetchRequest *)_fetchRequest {
NSMutableArray *predicates = [[NSMutableArray alloc] initWithCapacity:3];
- if (self.key) {
- NSPredicate *keyPredicate = [NSPredicate predicateWithFormat:@"%K == %@", _DCTImageCacheItemAttributes.key, self.key];
+ NSString *key = [self _key];
+ if (key.length > 0) {
+ NSPredicate *keyPredicate = [NSPredicate predicateWithFormat:@"%K == %@", _DCTImageCacheItemAttributes.key, key];
[predicates addObject:keyPredicate];
}
- if (!CGSizeEqualToSize(self.size, DCTImageCacheAttributesNullSize)) {
- NSPredicate *sizePredicate = [NSPredicate predicateWithFormat:@"%K == %@", _DCTImageCacheItemAttributes.sizeString, NSStringFromCGSize(self.size)];
+ NSString *sizeString = [self _sizeString];
+ if (sizeString.length > 0) {
+ NSPredicate *sizePredicate = [NSPredicate predicateWithFormat:@"%K == %@", _DCTImageCacheItemAttributes.sizeString, sizeString];
[predicates addObject:sizePredicate];
}
- if (self.createdBefore) {
- NSPredicate *datePredicate = [NSPredicate predicateWithFormat:@"%K < %@", _DCTImageCacheItemAttributes.date, self.createdBefore];
+ NSDate *createdBefore = [self _createdBefore];
+ if (createdBefore) {
+ NSPredicate *datePredicate = [NSPredicate predicateWithFormat:@"%K < %@", _DCTImageCacheItemAttributes.date, createdBefore];
[predicates addObject:datePredicate];
}
@@ -56,8 +65,22 @@ - (NSFetchRequest *)_fetchRequest {
}
- (void)_setupCacheItemProperties:(_DCTImageCacheItem *)cacheItem {
- cacheItem.key = self.key;
- cacheItem.sizeString = NSStringFromCGSize(self.size);
+ cacheItem.key = [self _key];
+ cacheItem.sizeString = [self _sizeString];
+}
+
+- (NSString *)_sizeString {
+ NSValue *value = [self.dictionary objectForKey:DCTImageCacheAttributesSize];
+ if (!value) return nil;
+ return NSStringFromCGSize([value CGSizeValue]);
+}
+
+- (NSString *)_key {
+ return [self.dictionary objectForKey:DCTImageCacheAttributesKey];
+}
+
+- (NSDate *)_createdBefore {
+ return [self.dictionary objectForKey:DCTImageCacheAttributesCreatedBefore];
}
@end

0 comments on commit 410888c

Please sign in to comment.