NSManagedObjectModel.m
1 /* Copyright (c) 2008 Dan Knapp 2 3 Permission is hereby granted, free of charge, to any person obtaining a copy of 4 this software and associated documentation files (the "Software"), to deal in 5 the Software without restriction, including without limitation the rights to 6 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 7 the Software, and to permit persons to whom the Software is furnished to do so, 8 subject to the following conditions: 9 10 The above copyright notice and this permission notice shall be included in all 11 copies or substantial portions of the Software. 12 13 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 15 FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 16 COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 17 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 18 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ 19 #import <CoreData/NSEntityDescription.h> 20 #import <CoreData/NSManagedObjectModel.h> 21 22 #import <Foundation/NSKeyedUnarchiver.h> 23 #import <Foundation/NSRaise.h> 24 25 @implementation NSManagedObjectModel 26 27 + (NSManagedObjectModel *) modelByMergingModels: (NSArray *) models { 28 NSManagedObjectModel *result = [[NSManagedObjectModel alloc] init]; 29 NSMutableArray *entities = [NSMutableArray array]; 30 31 for (NSManagedObjectModel *merge in models) { 32 [entities addObjectsFromArray: [merge entities]]; 33 } 34 35 [result setEntities: entities]; 36 37 return result; 38 } 39 40 + (NSManagedObjectModel *) mergedModelFromBundles: (NSArray *) bundles { 41 NSMutableArray *models = [NSMutableArray array]; 42 43 if (bundles == nil) 44 bundles = [NSArray arrayWithObject: [NSBundle mainBundle]]; 45 46 for (NSBundle *bundle in bundles) { 47 NSArray *moms = [bundle pathsForResourcesOfType: @"mom" 48 inDirectory: nil]; 49 50 for (NSString *path in moms) { 51 NSURL *url = [NSURL fileURLWithPath: path]; 52 NSManagedObjectModel *model = 53 [[NSManagedObjectModel alloc] initWithContentsOfURL: url]; 54 55 if (model == nil) 56 NSLog(@"-[%@ initWithContentsOfURL:] failed. url=%@", self, 57 url); 58 59 if (model != nil) 60 [models addObject: model]; 61 } 62 } 63 64 return [self modelByMergingModels: models]; 65 } 66 67 - init { 68 _entities = [[NSMutableDictionary alloc] init]; 69 _fetchRequestTemplates = [[NSMutableDictionary alloc] init]; 70 _configurations = [[NSMutableDictionary alloc] init]; 71 return self; 72 } 73 74 - initWithCoder: (NSCoder *) coder { 75 if (![coder allowsKeyedCoding]) 76 [NSException raise: NSInvalidArgumentException 77 format: @"%@ can not initWithCoder:%@", [self class], 78 [coder class]]; 79 80 _entities = [[coder decodeObjectForKey: @"NSEntities"] retain]; 81 for (NSEntityDescription *entity in [_entities allValues]) 82 [_entities setObject: entity forKey: [[entity name] uppercaseString]]; 83 84 _fetchRequestTemplates = 85 [[coder decodeObjectForKey: @"NSFetchRequestTemplates"] retain]; 86 _versionIdentifiers = 87 [[coder decodeObjectForKey: @"NSVersionIdentifiers"] retain]; 88 89 return self; 90 } 91 92 - initWithContentsOfURL: (NSURL *) url { 93 [self dealloc]; 94 95 NSData *data = [[NSData alloc] initWithContentsOfURL: url]; 96 97 if (data == nil) 98 return nil; 99 100 NSKeyedUnarchiver *unarchiver = 101 [[NSKeyedUnarchiver alloc] initForReadingWithData: data]; 102 NSManagedObjectModel *result = [unarchiver decodeObjectForKey: @"root"]; 103 104 [unarchiver release]; 105 [data release]; 106 107 return result; 108 } 109 110 - (NSArray *) entities { 111 return [_entities allValues]; 112 } 113 114 - (NSDictionary *) entitiesByName { 115 return _entities; 116 } 117 118 - (NSDictionary *) localizationDictionary { 119 return _localizationDictionary; 120 } 121 122 - (void) setEntities: (NSArray *) entities { 123 [_entities removeAllObjects]; 124 125 for (NSEntityDescription *entity in entities) { 126 [_entities setObject: entity forKey: [entity name]]; 127 [_entities setObject: entity forKey: [[entity name] uppercaseString]]; 128 } 129 } 130 131 - (void) setLocalizationDictionary: (NSDictionary *) dictionary { 132 dictionary = [dictionary copy]; 133 [_localizationDictionary release]; 134 _localizationDictionary = dictionary; 135 } 136 137 - (NSArray *) configurations { 138 return [_configurations allKeys]; 139 } 140 141 - (NSArray *) entitiesForConfiguration: (NSString *) configuration { 142 return [_configurations objectForKey: configuration]; 143 } 144 145 - (void) setEntities: (NSArray *) entities 146 forConfiguration: (NSString *) configuration 147 { 148 [_configurations setObject: entities forKey: configuration]; 149 } 150 151 - (NSFetchRequest *) fetchRequestTemplateForName: (NSString *) name { 152 return [_fetchRequestTemplates objectForKey: name]; 153 } 154 155 - (NSFetchRequest *) fetchRequestFromTemplateWithName: (NSString *) name 156 substitutionVariables: 157 (NSDictionary *) variables 158 { 159 NSUnimplementedMethod(); 160 } 161 162 - (void) setFetchRequestTemplate: (NSFetchRequest *) fetchRequest 163 forName: (NSString *) name 164 { 165 [_fetchRequestTemplates setObject: fetchRequest forKey: name]; 166 } 167 168 @end