Created
February 22, 2012 16:04
-
-
Save johnclayton/1885738 to your computer and use it in GitHub Desktop.
An implementation of Ruby's OStruct in Objective C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// FSQOpenStruct.h | |
// FivesquareKit | |
// | |
// Created by John Clayton on 2/5/12. | |
// Copyright (c) 2012 Fivesquare Software, LLC. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
@interface FSQOpenStruct : NSObject | |
@property (strong, readonly) NSMutableDictionary *attributes; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// FSQOpenStruct.m | |
// FivesquareKit | |
// | |
// Created by John Clayton on 2/5/12. | |
// Copyright (c) 2012 Fivesquare Software, LLC. All rights reserved. | |
// | |
#import "FSQOpenStruct.h" | |
#import "NSDictionary+FSQFoundation.h" | |
@implementation FSQOpenStruct | |
@synthesize attributes=attributes_; | |
- (NSMutableDictionary *) attributes { | |
@synchronized(self) { | |
if (nil == attributes_) { | |
attributes_ = [NSMutableDictionary new]; | |
} | |
} | |
return attributes_; | |
} | |
- (id)initWithAttributes:(NSDictionary *)attributes { | |
self = [super init]; | |
if (self) { | |
attributes_ = [attributes mutableDeepCopy]; | |
} | |
return self; | |
} | |
- (NSString *) description { | |
return [NSString stringWithFormat:@"%@ : %@", [super description], [self.attributes description]]; | |
} | |
// ========================================================================== // | |
#pragma mark - KVC | |
- (id)valueForKey:(NSString *)key { | |
id value = [self.attributes valueForKey:key]; | |
if (nil == value) { | |
value = [NSMutableDictionary new]; | |
[self.attributes setObject:value forKey:key]; | |
} | |
return value; | |
} | |
- (void)setValue:(id)value forKey:(NSString *)key { | |
[self willChangeValueForKey:key]; | |
[self.attributes setValue:value forKey:key]; | |
[self didChangeValueForKey:key]; | |
} | |
- (NSDictionary *)dictionaryWithValuesForKeys:(NSArray *)keys { | |
return [self.attributes dictionaryWithValuesForKeys:keys]; | |
} | |
- (void)setValuesForKeysWithDictionary:(NSDictionary *)keyedValues { | |
for (NSString *key in keyedValues) { | |
[self willChangeValueForKey:key]; | |
} | |
[self.attributes setValuesForKeysWithDictionary:keyedValues]; | |
for (NSString *key in keyedValues) { | |
[self didChangeValueForKey:key]; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// NSDictionary+FSQFoundation.h | |
// FivesquareKit | |
// | |
// Created by John Clayton on 2/5/12. | |
// Copyright (c) 2012 Fivesquare Software, LLC. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
@interface NSDictionary (FSQFoundation) | |
- (NSDictionary *) deepCopy; | |
- (NSMutableDictionary *) mutableDeepCopy; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// NSDictionary+FSQFoundation.m | |
// FivesquareKit | |
// | |
// Created by John Clayton on 2/5/12. | |
// Copyright (c) 2012 Fivesquare Software, LLC. All rights reserved. | |
// | |
#import "NSDictionary+FSQFoundation.h" | |
@implementation NSDictionary (FSQFoundation) | |
- (NSDictionary *) deepCopy { | |
NSMutableDictionary *deepCopy = [[NSMutableDictionary alloc] init]; | |
for (NSString *key in self) { | |
id value = [self valueForKey:key]; | |
[deepCopy setValue:[value copy] forKey:key]; | |
} | |
return deepCopy; | |
} | |
- (NSMutableDictionary *) mutableDeepCopy { | |
return (NSMutableDictionary *)[self deepCopy]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment