forked from nicklockwood/BaseModel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseModel.m
More file actions
executable file
·1057 lines (910 loc) · 30.8 KB
/
BaseModel.m
File metadata and controls
executable file
·1057 lines (910 loc) · 30.8 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// BaseModel.m
//
// Version 2.6.3
//
// Created by Nick Lockwood on 25/06/2011.
// Copyright 2011 Charcoal Design
//
// Distributed under the permissive zlib license
// Get the latest version from here:
//
// https://github.com/nicklockwood/BaseModel
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would be
// appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
#import "BaseModel.h"
#import <objc/message.h>
#import <Availability.h>
#if !__has_feature(objc_arc)
#error This class requires automatic reference counting
#endif
NSString *const BaseModelSharedInstanceUpdatedNotification = @"BaseModelSharedInstanceUpdatedNotification";
NSString *const BaseModelException = @"BaseModelException";
static NSString *const BMDateFormatterKey = @"BMDateFormatterKey";
static const void *BMAllPropertiesKey = &BMAllPropertiesKey;
static const void *BMCodablePropertiesKey = &BMCodablePropertiesKey;
static const void *BMSharedInstanceKey = &BMSharedInstanceKey;
static const void *BMLoadingFromResourceFileKey = &BMLoadingFromResourceFileKey;
static const void *BMWasSetUpKey = &BMWasSetUpKey;
static const NSUInteger BMStringDescriptionMaxLength = 16;
@implementation NSObject (BaseModel)
- (id)BM_propertyListRepresentation
{
return [NSKeyedArchiver archivedDataWithRootObject:self];
}
- (id)BM_JSONRepresentation
{
return [[NSKeyedArchiver archivedDataWithRootObject:self] BM_JSONRepresentation];
}
- (id)BM_objectFromPropertyListOrJSON
{
return self;
}
- (NSString *)BM_shortDescription
{
return [NSString stringWithFormat:@"<%@: %p>", [self classForCoder], self];
}
@end
@implementation NSString (BaseModel)
- (id)BM_propertyListRepresentation
{
return self;
}
- (id)BM_JSONRepresentation
{
return self;
}
- (NSString *)BM_shortDescription
{
NSString *string = self;
if ([string length] > BMStringDescriptionMaxLength)
{
string = [[string substringToIndex:BMStringDescriptionMaxLength - 1] stringByAppendingString:@"…"];
}
return [NSString stringWithFormat:@"\"%@\"", string];
}
@end
@implementation NSNumber (BaseModel)
- (id)BM_propertyListRepresentation
{
return self;
}
- (id)BM_JSONRepresentation
{
return self;
}
- (NSString *)BM_shortDescription
{
return [self description];
}
@end
@implementation NSData (BaseModel)
- (id)BM_propertyListRepresentation
{
return self;
}
- (id)BM_JSONRepresentation
{
NSString *base64String = nil;
#if (defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED < __MAC_10_9) || \
(defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_7_0)
if (![self respondsToSelector:@selector(base64EncodedStringWithOptions:)])
{
base64String = [self base64Encoding];
}
else
#endif
{
base64String = [self base64EncodedStringWithOptions:(NSDataBase64EncodingOptions)0];
}
return base64String;
}
- (id)BM_objectFromPropertyListOrJSON
{
//attempt to deserialise data as a plist
NSDictionary *object = [NSPropertyListSerialization propertyListWithData:self options:0 format:NULL error:NULL];
if ([object respondsToSelector:@selector(objectForKey:)] && object[@"$archiver"])
{
//unarchive object
return [NSKeyedUnarchiver unarchiveObjectWithData:self];
}
return self;
}
@end
@implementation NSDate (BaseModel)
- (id)BM_propertyListRepresentation
{
return self;
}
- (id)BM_JSONRepresentation
{
NSDateFormatter *formatter = [[NSThread currentThread] threadDictionary][BMDateFormatterKey];
if (!formatter)
{
formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
[[NSThread currentThread] threadDictionary][BMDateFormatterKey] = formatter;
}
return [formatter stringFromDate:self];
}
- (NSString *)BM_shortDescription
{
return [self description];
}
@end
@implementation NSArray (BaseModel)
- (id)BM_propertyListRepresentation
{
NSMutableArray *copy = [NSMutableArray arrayWithCapacity:[self count]];
[self enumerateObjectsUsingBlock:^(__unsafe_unretained id obj, __unused NSUInteger idx, __unused BOOL *stop) {
[copy addObject:[obj BM_propertyListRepresentation]];
}];
return copy;
}
- (id)BM_JSONRepresentation
{
NSMutableArray *copy = [NSMutableArray arrayWithCapacity:[self count]];
[self enumerateObjectsUsingBlock:^(__unsafe_unretained id obj, __unused NSUInteger idx, __unused BOOL *stop) {
[copy addObject:[obj BM_JSONRepresentation]];
}];
return copy;
}
- (id)BM_objectFromPropertyListOrJSON
{
NSMutableArray *copy = [NSMutableArray arrayWithCapacity:[self count]];
[self enumerateObjectsUsingBlock:^(__unsafe_unretained id obj, __unused NSUInteger idx, __unused BOOL *stop) {
[copy addObject:[obj BM_objectFromPropertyListOrJSON]];
}];
return copy;
}
@end
@implementation NSDictionary (BaseModel)
- (id)BM_propertyListRepresentation
{
NSMutableDictionary *copy = [NSMutableDictionary dictionaryWithCapacity:[self count]];
[self enumerateKeysAndObjectsUsingBlock:^(__unsafe_unretained id key, __unsafe_unretained id obj, __unused BOOL *stop) {
copy[key] = [obj BM_propertyListRepresentation];
}];
return copy;
}
- (id)BM_JSONRepresentation
{
NSMutableDictionary *copy = [NSMutableDictionary dictionaryWithCapacity:[self count]];
[self enumerateKeysAndObjectsUsingBlock:^(__unsafe_unretained id key, __unsafe_unretained id obj, __unused BOOL *stop) {
copy[key] = [obj BM_JSONRepresentation];
}];
return copy;
}
- (id)BM_objectFromPropertyListOrJSON
{
NSMutableDictionary *copy = [NSMutableDictionary dictionaryWithCapacity:[self count]];
[self enumerateKeysAndObjectsUsingBlock:^(__unsafe_unretained id key, __unsafe_unretained id obj, __unused BOOL *stop) {
copy[key] = [obj BM_objectFromPropertyListOrJSON];
}];
return copy;
}
@end
@implementation BaseModel
#pragma mark -
#pragma mark Private utility methods
+ (NSString *)BM_resourceFilePath:(NSString *)path
{
//check if the path is a full path or not
if (![path isAbsolutePath])
{
return [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:path];
}
return path;
}
+ (NSString *)BM_resourceFilePath
{
return [self BM_resourceFilePath:[self resourceFile]];
}
+ (NSString *)BM_saveFilePath:(NSString *)path
{
//check if the path is a full path or not
if (![path isAbsolutePath])
{
//get the path to the application support folder
NSString *folder = [NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES) lastObject];
#ifndef __IPHONE_OS_VERSION_MAX_ALLOWED
//append application name on Mac OS
NSString *identifier = [[NSBundle mainBundle] objectForInfoDictionaryKey:(NSString *)kCFBundleNameKey];
folder = [folder stringByAppendingPathComponent:identifier];
#endif
//create the folder if it doesn't exist
if (![[NSFileManager defaultManager] fileExistsAtPath:folder])
{
[[NSFileManager defaultManager] createDirectoryAtPath:folder
withIntermediateDirectories:YES
attributes:nil
error:NULL];
}
return [folder stringByAppendingPathComponent:path];
}
return path;
}
+ (NSString *)BM_saveFilePath
{
return [self BM_saveFilePath:[self saveFile]];
}
- (NSDictionary *)BM_propertyListRepresentation
{
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
for (__unsafe_unretained NSString *key in [[self class] codablePropertyKeys])
{
id value = [self valueForKey:key];
if (value) dict[key] = [(NSObject *)value BM_propertyListRepresentation];
}
return dict;
}
- (NSDictionary *)BM_JSONRepresentation
{
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
for (__unsafe_unretained NSString *key in [[self class] codablePropertyKeys])
{
id value = [self valueForKey:key];
if (value) dict[key] = [(NSObject *)value BM_JSONRepresentation];
}
return dict;
}
- (void)BM_setUp
{
objc_setAssociatedObject(self, BMWasSetUpKey, @YES, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
[self setUp];
}
- (void)BM_tearDown
{
[self tearDown];
}
#pragma mark -
#pragma mark Description
- (NSString *)debugDescription
{
NSMutableString *description = [NSMutableString stringWithFormat:@"<%@: %p", [self class], self];
[[self dictionaryRepresentation] enumerateKeysAndObjectsUsingBlock:^(NSString *key, id obj, __unused BOOL *stop) {
[description appendFormat:@"; %@ = %@", key, [obj BM_shortDescription]];
}];
[description appendString:@">"];
return description;
}
#pragma mark -
#pragma mark Singleton behaviour
+ (void)setSharedInstance:(BaseModel *)instance
{
if (instance && ![instance isKindOfClass:self])
{
[NSException raise:BaseModelException format:@"setSharedInstance: instance class does not match"];
}
id oldInstance = objc_getAssociatedObject(self, BMSharedInstanceKey);
objc_setAssociatedObject(self, BMSharedInstanceKey, instance, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
if (oldInstance)
{
void (^update)() = ^{
[[NSNotificationCenter defaultCenter] postNotificationName:BaseModelSharedInstanceUpdatedNotification object:oldInstance];
};
if ([NSThread currentThread] == [NSThread mainThread])
{
update();
}
else
{
dispatch_async(dispatch_get_main_queue(), update);
}
}
}
+ (BOOL)hasSharedInstance
{
return objc_getAssociatedObject(self, BMSharedInstanceKey) != nil;
}
+ (instancetype)sharedInstance
{
@synchronized ([self class])
{
id instance = objc_getAssociatedObject(self, BMSharedInstanceKey);
if (instance == nil)
{
//load or create instance
[self reloadSharedInstance];
//get loaded instance
instance = objc_getAssociatedObject(self, BMSharedInstanceKey);
}
return instance;
}
}
+ (void)reloadSharedInstance
{
id instance = nil;
//try loading previously saved version
if ([self saveFormat] == BMFileFormatUserDefaults)
{
instance = [self instanceWithObject:[[NSUserDefaults standardUserDefaults] dictionaryRepresentation]];
}
else if ([self saveFormat] == BMFileFormatKeychain)
{
Class keychainClass = NSClassFromString(@"FXKeychain");
NSAssert(keychainClass, @"FXKeychain library was not found");
id defaultKeychain = [keychainClass valueForKey:@"defaultKeychain"];
NSMutableDictionary *properties = [NSMutableDictionary dictionary];
for (NSString *key in [self codablePropertyKeys])
{
id value = defaultKeychain[key];
if (value) properties[key] = value;
}
instance = [self instanceWithObject:properties];
}
else
{
instance = [self instanceWithContentsOfFile:[self BM_saveFilePath]];
}
if (instance == nil)
{
//construct a new instance
instance = [self instance];
}
//set singleton
[self setSharedInstance:instance];
}
+ (NSString *)resourceFile
{
//used for every instance
return [NSStringFromClass(self) stringByAppendingPathExtension:@"plist"];
}
+ (NSString *)saveFile
{
//used to save shared (singleton) instance
NSString *extension = nil;
switch ([self saveFormat])
{
case BMFileFormatKeyedArchive:
case BMFileFormatXMLPropertyList:
case BMFileFormatBinaryPropertyList:
case BMFileFormatCryptoCoding:
case BMFileFormatHRCodedXML:
case BMFileFormatHRCodedBinary:
{
extension = @"plist";
break;
}
case BMFileFormatJSON:
case BMFileFormatHRCodedJSON:
{
extension = @"json";
break;
}
case BMFileFormatFastCoding:
{
extension = @"fast";
break;
}
case BMFileFormatUserDefaults:
case BMFileFormatKeychain:
{
return nil;
}
}
return [NSStringFromClass(self) stringByAppendingPathExtension:extension];
}
+ (BMFileFormat)saveFormat
{
if ([self respondsToSelector:NSSelectorFromString(@"useHRCoderIfAvailable")])
{
[NSException raise:BaseModelException format:@"You are using the deprecated useHRCoderIfAvailable method instead of specifying the saveFormat as BMFileFormatHRCodedBinary."];
}
if ([self respondsToSelector:NSSelectorFromString(@"CCPassword")])
{
NSLog(@"You have implemented the CCPassword method without specifying the saveFormat as BMFileFormatCryptoCoding. This is a warning for now, but may become an error in future.");
return BMFileFormatCryptoCoding;
}
return BMFileFormatKeyedArchive;
}
- (BOOL)save
{
if (objc_getAssociatedObject([self class], BMSharedInstanceKey) == self)
{
//shared (singleton) instance
if ([[self class] saveFormat] == BMFileFormatUserDefaults)
{
[[NSUserDefaults standardUserDefaults] setValuesForKeysWithDictionary:[self BM_propertyListRepresentation]];
[[NSUserDefaults standardUserDefaults] synchronize];
return YES;
}
else if ([[self class] saveFormat] == BMFileFormatKeychain)
{
Class keychainClass = NSClassFromString(@"FXKeychain");
NSAssert(keychainClass, @"FXKeychain library was not found");
id defaultKeychain = [keychainClass valueForKey:@"defaultKeychain"];
for (NSString *key in [[self class] codablePropertyKeys])
{
id value = [self valueForKey:key];
if (value) defaultKeychain[key] = value;
}
return YES;
}
else
{
return [self writeToFile:[[self class] BM_saveFilePath] atomically:YES];
}
}
else
{
//no save implementation
[NSException raise:BaseModelException format:@"Unable to save object, save method not implemented"];
return NO;
}
}
#pragma mark -
#pragma mark Default constructors
- (void)setUp
{
//override this
}
- (void)setWithCoder:(NSCoder *)coder
{
for (__unsafe_unretained NSString *key in [[self class] codablePropertyKeys])
{
id value = [coder decodeObjectForKey:key];
if (value) [self setValue:value forKey:key];
}
}
- (void)setWithDictionary:(NSDictionary *)dict
{
[dict enumerateKeysAndObjectsUsingBlock:^(__unsafe_unretained id key, __unsafe_unretained id obj, __unused BOOL *stop) {
[self setValue:[obj BM_objectFromPropertyListOrJSON] forKey:key];
}];
}
- (void)setValue:(__unused id)value forUndefinedKey:(__unused NSString *)key
{
//fail silently
}
- (id)valueForUndefinedKey:(__unused NSString *)key
{
//return nothing
return nil;
}
- (void)setNilValueForKey:(NSString *)key
{
//handle gracefully
[self setValue:@0 forKey:key];
}
+ (instancetype)instance
{
return [[self alloc] init];
}
- (instancetype)init
{
@synchronized ([self class])
{
if (!objc_getAssociatedObject([self class], BMLoadingFromResourceFileKey))
{
//attempt to load from resource file
objc_setAssociatedObject([self class], BMLoadingFromResourceFileKey, @YES, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
id object = [(BaseModel *)[[self class] alloc] initWithContentsOfFile:[[self class] BM_resourceFilePath]];
objc_setAssociatedObject([self class], BMLoadingFromResourceFileKey, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
if (object)
{
return ((self = object));
}
}
if ((self = [super init]))
{
if ([self class] == [BaseModel class])
{
[NSException raise:BaseModelException format:@"BaseModel class is abstract and should be subclassed rather than instantiated directly"];
}
[self BM_setUp];
}
return self;
}
}
- (void)tearDown
{
//override this
}
- (void)dealloc
{
if (objc_getAssociatedObject(self, BMWasSetUpKey))
{
[self BM_tearDown];
}
}
+ (instancetype)instanceWithObject:(id)object
{
//return nil if object is nil
return object? [[self alloc] initWithObject:object]: nil;
}
- (NSString *)setterNameForClass:(Class)class
{
//get class name
NSString *className = NSStringFromClass(class);
//strip NS prefix
if ([className hasPrefix:@"NS"])
{
className = [className substringFromIndex:2];
}
//return setter name
return [NSString stringWithFormat:@"setWith%@:", className];
}
- (instancetype)initWithObject:(id)object
{
if (!object)
{
return nil;
}
if ((self = [self init]))
{
Class class = [object class];
while (true)
{
SEL setter = NSSelectorFromString([self setterNameForClass:class]);
if ([self respondsToSelector:setter])
{
((void (*)(id, SEL, id))objc_msgSend)(self, setter, object);
return self;
}
if ([class superclass] == [NSObject class]) break;
class = [class superclass];
}
if ([object isKindOfClass:[NSNull class]])
{
return nil;
}
[NSException raise:BaseModelException format:@"%@ not implemented", [self setterNameForClass:class]];
}
return self;
}
+ (NSArray *)instancesWithArray:(NSArray *)array
{
NSMutableArray *result = [NSMutableArray array];
for (__unsafe_unretained id object in array)
{
id instance = [self instanceWithObject:object];
if (instance) [result addObject:instance];
}
return result;
}
+ (instancetype)instanceWithCoder:(NSCoder *)decoder
{
//return nil if coder is nil
return decoder? [[self alloc] initWithCoder:decoder]: nil;
}
- (instancetype)initWithCoder:(NSCoder *)decoder
{
if ((self = [self init]))
{
[self setWithCoder:decoder];
}
return self;
}
+ (instancetype)instanceWithContentsOfFile:(NSString *)filePath
{
//check if the path is a full path or not
NSString *path = filePath;
if (![path isAbsolutePath])
{
//try resources
path = [self BM_resourceFilePath:filePath];
if (![[NSFileManager defaultManager] fileExistsAtPath:path])
{
//try application support
path = [self BM_saveFilePath:filePath];
}
}
//return nil if file does not exist
return [[NSFileManager defaultManager] fileExistsAtPath:path]? [(BaseModel *)[self alloc] initWithContentsOfFile:path]: nil;
}
- (instancetype)initWithContentsOfFile:(NSString *)filePath
{
id object = nil;
NSData *data = nil;
static NSCache *cachedResourceFiles = nil;
if (cachedResourceFiles == nil)
{
cachedResourceFiles = [[NSCache alloc] init];
}
//check cache for existing instance
//only cache files inside the main bundle as they are immutable
BOOL isResourceFile = [filePath hasPrefix:[[NSBundle mainBundle] bundlePath]];
if (isResourceFile)
{
object = [cachedResourceFiles objectForKey:filePath];
if (object == [NSNull null])
{
object = nil;
}
else if ([object isKindOfClass:[NSData class]])
{
data = object;
object = nil;
}
}
//load object if no cached version found
if (!object)
{
if (!data)
{
//load the file
data = [NSData dataWithContentsOfFile:filePath];
}
if (data)
{
//attempt to guess file type
char byte = *((char *)data.bytes);
if (byte == 'T')
{
//attempt to deserialise using FastCoding
Class coderClass = NSClassFromString(@"FastCoder");
object = ((id (*)(id, SEL, id))objc_msgSend)(coderClass, NSSelectorFromString(@"objectWithData:"), data);
}
if (!object && (byte == '{' || byte == '[' || byte == '"' || byte == 'n'))
{
//attempt to deserialise data as json
object = [NSJSONSerialization JSONObjectWithData:data
options:NSJSONReadingAllowFragments
error:NULL];
}
if (!object)
{
//attempt to deserialise data as a plist
NSPropertyListFormat format;
NSPropertyListReadOptions options = NSPropertyListMutableContainersAndLeaves;
object = [NSPropertyListSerialization propertyListWithData:data options:options format:&format error:NULL];
}
if (!object)
{
//data is not a known serialisation format
object = data;
}
}
}
//success?
if (object)
{
//check if object is an NSCoded archive
if ([object respondsToSelector:@selector(objectForKey:)])
{
if (object[@"$archiver"])
{
if (isResourceFile)
{
//cache data for next time
[cachedResourceFiles setObject:data forKey:filePath];
}
//unarchive object
Class coderClass = NSClassFromString(@"CryptoCoder");
if (!coderClass)
{
coderClass = [NSKeyedUnarchiver class];
}
object = [coderClass unarchiveObjectWithData:data];
}
else
{
if (isResourceFile)
{
//cache object for next time
[cachedResourceFiles setObject:object forKey:filePath];
}
//unarchive object
Class HRCoderClass = NSClassFromString(@"HRCoder");
NSString *classNameKey = [HRCoderClass valueForKey:@"classNameKey"];
if (object[classNameKey])
{
SEL selector = NSSelectorFromString(@"unarchiveObjectWithPlistOrJSON:");
if (![HRCoderClass respondsToSelector:selector])
{
[NSException raise:BaseModelException format:@"This version of HRCoder is not compatibile with this version BaseModel. Please ensure you have upgraded both libraries to the latest version."];
}
object = ((id (*)(id, SEL, id))objc_msgSend)(HRCoderClass, selector, object);
}
}
}
else if (isResourceFile)
{
//cache object for next time
[cachedResourceFiles setObject:object forKey:filePath];
}
if ([object isKindOfClass:[self class]])
{
//return object
return ((self = object));
}
//load with object
return ((self = [self initWithObject:object]));
}
else if (isResourceFile)
{
//store null for non-existent files to improve performance next time
[cachedResourceFiles setObject:[NSNull null] forKey:filePath];
}
//failed to load
return ((self = nil));
}
#pragma mark -
#pragma mark Property access
+ (NSArray *)allPropertyKeys
{
__autoreleasing NSArray *propertyKeys = objc_getAssociatedObject(self, BMAllPropertiesKey);
if (!propertyKeys)
{
NSMutableArray *keys = [NSMutableArray array];
NSMutableArray *codableKeys = [NSMutableArray array];
Class subclass = [self class];
while (subclass != [BaseModel class])
{
unsigned int propertyCount;
objc_property_t *properties = class_copyPropertyList(subclass, &propertyCount);
for (unsigned int i = 0; i < propertyCount; i++)
{
//get property
objc_property_t property = properties[i];
const char *propertyName = property_getName(property);
NSString *key = @(propertyName);
//add to properties
[keys addObject:key];
//see if there is a backing ivar
char *ivar = property_copyAttributeValue(property, "V");
if (ivar)
{
//check if ivar has KVC-compliant name
NSString *ivarName = @(ivar);
if ([ivarName isEqualToString:key] || [ivarName isEqualToString:[@"_" stringByAppendingString:key]])
{
//setValue:forKey: will work
[codableKeys addObject:key];
}
free(ivar);
}
}
free(properties);
NSArray *uncodable = [subclass uncodableProperties];
if ([uncodable count])
{
NSLog(@"You have implemented the AutoCoding +uncodableProperties method on the class %@. BaseModel supports this for now, but you should switch to using ivars for properties that you do not want to be saved instead.", subclass);
[codableKeys removeObjectsInArray:uncodable];
}
subclass = [subclass superclass];
}
//store keys
propertyKeys = [keys copy];
objc_setAssociatedObject(self, BMAllPropertiesKey, keys, OBJC_ASSOCIATION_RETAIN);
objc_setAssociatedObject(self, BMCodablePropertiesKey, codableKeys, OBJC_ASSOCIATION_COPY);
}
return propertyKeys;
}
+ (NSArray *)codablePropertyKeys
{
NSArray *codableKeys = objc_getAssociatedObject(self, BMCodablePropertiesKey);
if (!codableKeys)
{
[self allPropertyKeys];
return [self codablePropertyKeys];
}
return codableKeys;
}
+ (NSArray *)uncodableProperties
{
//DEPRECATED
return nil;
}
- (NSDictionary *)dictionaryRepresentation
{
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
for (__unsafe_unretained NSString *key in [[self class] allPropertyKeys])
{
id value = [self valueForKey:key];
if (value) dict[key] = value;
}
return dict;
}
#pragma mark -
#pragma mark Serializing
- (void)encodeWithCoder:(NSCoder *)coder
{
for (__unsafe_unretained NSString *key in [[self class] codablePropertyKeys])
{
id value = [self valueForKey:key];
if (value) [coder encodeObject:value forKey:key];
}
}
- (BOOL)writeToFile:(NSString *)path format:(BMFileFormat)format atomically:(BOOL)atomically
{
NSData *data = nil;
switch (format)
{
case BMFileFormatKeyedArchive:
{
data = [NSKeyedArchiver archivedDataWithRootObject:self];
break;
}
case BMFileFormatXMLPropertyList:
case BMFileFormatBinaryPropertyList:
case BMFileFormatUserDefaults:
case BMFileFormatKeychain:
{
NSPropertyListFormat plistFormat = (format == BMFileFormatXMLPropertyList)? NSPropertyListXMLFormat_v1_0: NSPropertyListBinaryFormat_v1_0;
data = [NSPropertyListSerialization dataWithPropertyList:[self BM_propertyListRepresentation] format:plistFormat options:0 error:NULL];
break;
}
case BMFileFormatJSON:
{
data = [NSJSONSerialization dataWithJSONObject:[self BM_JSONRepresentation] options:(NSJSONWritingOptions)0 error:NULL];
break;
}
case BMFileFormatCryptoCoding:
{
Class coderClass = NSClassFromString(@"CryptoCoder");
NSAssert(coderClass, @"CryptoCoding library was not found");
data = [coderClass archivedDataWithRootObject:self];
break;
}
case BMFileFormatHRCodedXML:
case BMFileFormatHRCodedJSON:
case BMFileFormatHRCodedBinary: