FEMMapping.m 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. // For License please refer to LICENSE file in the root of FastEasyMapping project
  2. #import "FEMMapping.h"
  3. #import "FEMManagedObjectMapping.h"
  4. #import "FEMObjectMapping.h"
  5. @implementation FEMMapping
  6. #pragma mark - Init
  7. - (instancetype)initWithObjectClass:(Class)objectClass {
  8. self = [super init];
  9. if (self) {
  10. _attributeMap = [NSMutableDictionary new];
  11. _relationshipMap = [NSMutableDictionary new];
  12. _objectClass = objectClass;
  13. }
  14. return self;
  15. }
  16. - (instancetype)initWithObjectClass:(Class)objectClass rootPath:(NSString *)rootPath {
  17. self = [self initWithObjectClass:objectClass];
  18. if (self) {
  19. self.rootPath = rootPath;
  20. }
  21. return self;
  22. }
  23. - (instancetype)initWithEntityName:(NSString *)entityName {
  24. self = [super init];
  25. if (self) {
  26. _attributeMap = [NSMutableDictionary new];
  27. _relationshipMap = [NSMutableDictionary new];
  28. _entityName = [entityName copy];
  29. }
  30. return self;
  31. }
  32. - (instancetype)initWithEntityName:(NSString *)entityName rootPath:(NSString *)rootPath {
  33. self = [self initWithEntityName:entityName];
  34. if (self) {
  35. self.rootPath = rootPath;
  36. }
  37. return self;
  38. }
  39. #pragma mark - Attribute Mapping
  40. - (void)addPropertyMapping:(id<FEMProperty>)propertyMapping toMap:(NSMutableDictionary *)map {
  41. NSParameterAssert(propertyMapping);
  42. NSAssert(
  43. propertyMapping.property.length > 0,
  44. @"It's illegal to add mapping without specified property:%@",
  45. propertyMapping
  46. );
  47. #ifdef DEBUG
  48. FEMAttribute *existingMapping = map[propertyMapping.property];
  49. if (existingMapping) {
  50. NSLog(@"%@ replacing %@ with %@", NSStringFromClass(self.class), existingMapping, propertyMapping);
  51. }
  52. #endif
  53. map[propertyMapping.property] = propertyMapping;
  54. }
  55. - (void)addAttribute:(FEMAttribute *)attribute {
  56. [self addPropertyMapping:attribute toMap:_attributeMap];
  57. }
  58. - (FEMAttribute *)attributeForProperty:(NSString *)property {
  59. return _attributeMap[property];
  60. }
  61. #pragma mark - Relationship Mapping
  62. - (void)addRelationship:(FEMRelationship *)relationship {
  63. [self addPropertyMapping:relationship toMap:_relationshipMap];
  64. }
  65. - (FEMRelationship *)relationshipForProperty:(NSString *)property {
  66. return _relationshipMap[property];
  67. }
  68. #pragma mark - Properties
  69. - (NSArray *)attributes {
  70. return [_attributeMap allValues];
  71. }
  72. - (NSArray *)relationships {
  73. return [_relationshipMap allValues];
  74. }
  75. #pragma mark -
  76. - (FEMAttribute *)primaryKeyAttribute {
  77. return _attributeMap[self.primaryKey];
  78. }
  79. #pragma mark - Description
  80. - (NSString *)description {
  81. NSMutableString *description = [NSMutableString stringWithFormat:
  82. @"<%@ %p>\n<%%@> {\nrootPath:%@\n",
  83. NSStringFromClass(self.class),
  84. (__bridge void *) self,
  85. self.rootPath
  86. ];
  87. [description appendString:@"attributes {\n"];
  88. for (FEMAttribute *mapping in self.attributes) {
  89. [description appendFormat:@"\t(%@),\n", [mapping description]];
  90. }
  91. [description appendString:@"}\n"];
  92. [description appendString:@"relationships {\n"];
  93. for (FEMRelationship *relationshipMapping in self.relationships) {
  94. [description appendFormat:@"\t(%@),", [relationshipMapping description]];
  95. }
  96. [description appendFormat:@"}\n"];
  97. return description;
  98. }
  99. @end
  100. @implementation FEMMapping (Shortcut)
  101. - (void)addAttributesFromDictionary:(NSDictionary *)attributesToKeyPath {
  102. [attributesToKeyPath enumerateKeysAndObjectsUsingBlock:^(id attribute, id keyPath, BOOL *stop) {
  103. [self addAttribute:[FEMAttribute mappingOfProperty:attribute toKeyPath:keyPath]];
  104. }];
  105. }
  106. - (void)addAttributesFromArray:(NSArray *)attributes {
  107. for (NSString *attribute in attributes) {
  108. [self addAttribute:[FEMAttribute mappingOfProperty:attribute toKeyPath:attribute]];
  109. }
  110. }
  111. - (void)addAttributeWithProperty:(NSString *)property keyPath:(NSString *)keyPath {
  112. [self addAttribute:[FEMAttribute mappingOfProperty:property toKeyPath:keyPath]];
  113. }
  114. - (void)addRelationshipMapping:(FEMMapping *)mapping forProperty:(NSString *)property keyPath:(NSString *)keyPath {
  115. FEMRelationship *relationship = [[FEMRelationship alloc] initWithProperty:property keyPath:keyPath mapping:mapping];
  116. [self addRelationship:relationship];
  117. }
  118. - (void)addToManyRelationshipMapping:(FEMMapping *)mapping forProperty:(NSString *)property keyPath:(NSString *)keyPath {
  119. FEMRelationship *relationship = [[FEMRelationship alloc] initWithProperty:property keyPath:keyPath mapping:mapping];
  120. relationship.toMany = YES;
  121. [self addRelationship:relationship];
  122. }
  123. @end
  124. @implementation FEMMapping (FEMManagedObjectMapping_Deprecated)
  125. + (FEMMapping *)mappingForEntityName:(NSString *)entityName {
  126. FEMMapping *mapping = [[FEMMapping alloc] initWithEntityName:entityName];
  127. return mapping;
  128. }
  129. + (FEMMapping *)mappingForEntityName:(NSString *)entityName
  130. configuration:(void (^)(FEMManagedObjectMapping *sender))configuration {
  131. FEMMapping *mapping = [[FEMMapping alloc] initWithEntityName:entityName];
  132. configuration(mapping);
  133. return mapping;
  134. }
  135. + (FEMMapping *)mappingForEntityName:(NSString *)entityName
  136. rootPath:(NSString *)rootPath
  137. configuration:(void (^)(FEMManagedObjectMapping *sender))configuration {
  138. FEMMapping *mapping = [[FEMMapping alloc] initWithEntityName:entityName rootPath:rootPath];
  139. configuration(mapping);
  140. return mapping;
  141. }
  142. @end
  143. @implementation FEMMapping (FEMObjectMapping_Deprecated)
  144. + (FEMMapping *)mappingForClass:(Class)objectClass configuration:(void (^)(FEMObjectMapping *mapping))configuration {
  145. FEMMapping *mapping = [[FEMMapping alloc] initWithObjectClass:objectClass];
  146. configuration(mapping);
  147. return mapping;
  148. }
  149. + (FEMMapping *)mappingForClass:(Class)objectClass rootPath:(NSString *)rootPath configuration:(void (^)(FEMObjectMapping *mapping))configuration {
  150. FEMMapping *mapping = [[FEMMapping alloc] initWithObjectClass:objectClass rootPath:rootPath];
  151. configuration(mapping);
  152. return mapping;
  153. }
  154. @end