Skip to content

Commit

Permalink
refactor incremental mapping to use a property on EKObjectMapping
Browse files Browse the repository at this point in the history
  • Loading branch information
DenTelezhkin committed Jan 23, 2015
1 parent cb27680 commit 9751407
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 200 deletions.
82 changes: 0 additions & 82 deletions EasyMapping/EKManagedObjectMapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,6 @@
withMapping:(EKManagedObjectMapping *)mapping
inManagedObjectContext:(NSManagedObjectContext*)context;

/**
Creates object from JSON representation, using `mapping` in `context`. If incrementalData is true, to-many relationship data is pushed to the existing data instead of replaced.
@param externalRepresentation JSON representation of object data
@param mapping object mapping
@param context managed object context to perform object creation
@param incrementalData Defines if to-many relationship data is pushed or replaced
@result mapped managed object
*/
+ (id)objectFromExternalRepresentation:(NSDictionary *)externalRepresentation
withMapping:(EKManagedObjectMapping *)mapping
inManagedObjectContext:(NSManagedObjectContext*)context
incrementalData:(BOOL)incrementalData;

/**
Fills previously existed object with values, provided in JSON representation. All values, that are included in mapping and were filled prior to calling this method, will be overwritten.
Expand All @@ -80,27 +62,6 @@
withMapping:(EKManagedObjectMapping *)mapping
inManagedObjectContext:(NSManagedObjectContext*)context;

/**
Fills previously existed object with values, provided in JSON representation. All values, that are included in mapping and were filled prior to calling this method, will be overwritten. If incrementalData is true, to-many relationship data is pushed to the existing data instead of replaced.
@param object Object to fill
@param externalRepresentation JSON representation of object data
@param mapping object mapping
@param context managed object context to perform object creation
@param incrementalData Defines if to-many relationship data is pushed or replaced
@result filled managed object
*/
+ (id) fillObject:(id)object
fromExternalRepresentation:(NSDictionary *)externalRepresentation
withMapping:(EKManagedObjectMapping *)mapping
inManagedObjectContext:(NSManagedObjectContext*)context
incrementalData:(BOOL)incrementalData;

/**
Create array of CoreData objects. If passed JSON contains primary keys, previously existing object with these keys will be updated. Simply put, this method uses Find-Or-Create pattern.
Expand All @@ -116,24 +77,6 @@
withMapping:(EKManagedObjectMapping *)mapping
inManagedObjectContext:(NSManagedObjectContext*)context;

/**
Create array of CoreData objects. If passed JSON contains primary keys, previously existing object with these keys will be updated. Simply put, this method uses Find-Or-Create pattern. If incrementalData is true, to-many relationship data is pushed to the existing data instead of replaced.
@param externalRepresentation JSON array with objects
@param mapping object mapping
@param context managed object context to perform objects creation
@param incrementalData Defines if to-many relationship data is pushed or replaced
@result array of managed objects
*/
+ (NSArray *)arrayOfObjectsFromExternalRepresentation:(NSArray *)externalRepresentation
withMapping:(EKManagedObjectMapping *)mapping
inManagedObjectContext:(NSManagedObjectContext*)context
incrementalData:(BOOL)incrementalData;

/**
Synchronize the objects in the managed object context with the objects from an external
representation. Any new objects will be created, any existing objects will be updated
Expand All @@ -155,29 +98,4 @@
fetchRequest:(NSFetchRequest*)fetchRequest
inManagedObjectContext:(NSManagedObjectContext *)context;

/**
Synchronize the objects in the managed object context with the objects from an external
representation. Any new objects will be created, any existing objects will be updated
and any object not present in the external representation will be deleted from the
managed object context. The fetch request is used to pre-fetch all existing objects.
If incrementalData is true, to-many relationship data is pushed to the existing data instead of replaced.
@param externalRepresentation JSON array with objects
@param mapping object mapping
@param fetchRequest Fetch request to get existing objects
@param context managed object context to perform objects creation
@param incrementalData Defines if to-many relationship data is pushed or replaced
@result array of managed objects
*/
+ (NSArray *)syncArrayOfObjectsFromExternalRepresentation:(NSArray *)externalRepresentation
withMapping:(EKManagedObjectMapping *)mapping
fetchRequest:(NSFetchRequest*)fetchRequest
inManagedObjectContext:(NSManagedObjectContext *)context
incrementalData:(BOOL)incrementalData;

@end
92 changes: 17 additions & 75 deletions EasyMapping/EKManagedObjectMapper.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ + (instancetype)mapperWithImporter:(EKCoreDataImporter *)importer

- (id)objectFromExternalRepresentation:(NSDictionary *)externalRepresentation
withMapping:(EKManagedObjectMapping *)mapping
incrementalData:(BOOL)incrementalData
{
NSManagedObject * object = [self.importer existingObjectForRepresentation:externalRepresentation
mapping:mapping
Expand All @@ -56,16 +55,14 @@ - (id)objectFromExternalRepresentation:(NSDictionary *)externalRepresentation
}
NSManagedObject * filledObject = [self fillObject:object
fromExternalRepresentation:externalRepresentation
withMapping:mapping
incrementalData:incrementalData];
withMapping:mapping];
[self.importer cacheObject:filledObject withMapping:mapping];

return filledObject;
}

- (id)fillObject:(id)object fromExternalRepresentation:(NSDictionary *)externalRepresentation
withMapping:(EKManagedObjectMapping *)mapping
incrementalData:(BOOL)incrementalData
{
NSDictionary * representation = [EKPropertyHelper extractRootPathFromExternalRepresentation:externalRepresentation
withMapping:mapping];
Expand All @@ -76,31 +73,30 @@ - (id)fillObject:(id)object fromExternalRepresentation:(NSDictionary *)externalR
fromRepresentation:representation
inContext:self.importer.context];
}];
[mapping.hasOneMappings enumerateKeysAndObjectsUsingBlock:^(id key, EKRelationshipMapping * mapping, BOOL * stop)
[mapping.hasOneMappings enumerateKeysAndObjectsUsingBlock:^(id key, EKRelationshipMapping * relationship, BOOL * stop)
{
NSDictionary * value = [mapping extractObjectFromRepresentation:representation];
NSDictionary * value = [relationship extractObjectFromRepresentation:representation];
if (value && value != (id)[NSNull null])
{
id result = [self objectFromExternalRepresentation:value withMapping:(EKManagedObjectMapping *)[mapping objectMapping] incrementalData:incrementalData];
[EKPropertyHelper setValue:result onObject:object forKeyPath:mapping.property];
id result = [self objectFromExternalRepresentation:value withMapping:(EKManagedObjectMapping *)[relationship objectMapping]];
[EKPropertyHelper setValue:result onObject:object forKeyPath:relationship.property];
}
}];
[mapping.hasManyMappings enumerateKeysAndObjectsUsingBlock:^(id key, EKRelationshipMapping * mapping, BOOL * stop)
[mapping.hasManyMappings enumerateKeysAndObjectsUsingBlock:^(id key, EKRelationshipMapping * relationship, BOOL * stop)
{
NSArray * arrayToBeParsed = [representation valueForKeyPath:key];
if (arrayToBeParsed && arrayToBeParsed != (id)[NSNull null])
{
NSArray * parsedArray = [self arrayOfObjectsFromExternalRepresentation:arrayToBeParsed
withMapping:(EKManagedObjectMapping *)[mapping objectMapping]
incrementalData:incrementalData];
withMapping:(EKManagedObjectMapping *)[relationship objectMapping]];
id parsedObjects = [EKPropertyHelper propertyRepresentation:parsedArray
forObject:object
withPropertyName:[mapping property]];
if(incrementalData) {
[EKPropertyHelper addValue:parsedObjects onObject:object forKeyPath:mapping.property];
withPropertyName:[relationship property]];
if(mapping.incrementalData) {
[EKPropertyHelper addValue:parsedObjects onObject:object forKeyPath:relationship.property];
}
else {
[EKPropertyHelper setValue:parsedObjects onObject:object forKeyPath:mapping.property];
[EKPropertyHelper setValue:parsedObjects onObject:object forKeyPath:relationship.property];
}
}
}];
Expand All @@ -109,13 +105,12 @@ - (id)fillObject:(id)object fromExternalRepresentation:(NSDictionary *)externalR

- (NSArray *)arrayOfObjectsFromExternalRepresentation:(NSArray *)externalRepresentation
withMapping:(EKManagedObjectMapping *)mapping
incrementalData:(BOOL)incrementalData
{

NSMutableArray * array = [NSMutableArray array];
for (NSDictionary * representation in externalRepresentation)
{
id parsedObject = [self objectFromExternalRepresentation:representation withMapping:mapping incrementalData:incrementalData];
id parsedObject = [self objectFromExternalRepresentation:representation withMapping:mapping];
[array addObject:parsedObject];
}
return [NSArray arrayWithArray:array];
Expand All @@ -124,7 +119,6 @@ - (NSArray *)arrayOfObjectsFromExternalRepresentation:(NSArray *)externalReprese
- (NSArray *)syncArrayOfObjectsFromExternalRepresentation:(NSArray *)externalRepresentation
withMapping:(EKManagedObjectMapping *)mapping
fetchRequest:(NSFetchRequest *)fetchRequest
incrementalData:(BOOL)incrementalData
{
NSAssert(mapping.primaryKey, @"A mapping with a primary key is required");
EKPropertyMapping * primaryKeyPropertyMapping = [mapping primaryKeyPropertyMapping];
Expand All @@ -148,7 +142,7 @@ - (NSArray *)syncArrayOfObjectsFromExternalRepresentation:(NSArray *)externalRep
object = [NSEntityDescription insertNewObjectForEntityForName:mapping.entityName
inManagedObjectContext:self.importer.context];

[self fillObject:object fromExternalRepresentation:representation withMapping:mapping incrementalData:incrementalData];
[self fillObject:object fromExternalRepresentation:representation withMapping:mapping];
[array addObject:object];
}

Expand All @@ -170,17 +164,6 @@ - (NSArray *)syncArrayOfObjectsFromExternalRepresentation:(NSArray *)externalRep
+ (id)objectFromExternalRepresentation:(NSDictionary *)externalRepresentation
withMapping:(EKManagedObjectMapping *)mapping
inManagedObjectContext:(NSManagedObjectContext *)context
{
return [self objectFromExternalRepresentation:externalRepresentation
withMapping:mapping
inManagedObjectContext:context
incrementalData:NO];
}

+ (id)objectFromExternalRepresentation:(NSDictionary *)externalRepresentation
withMapping:(EKManagedObjectMapping *)mapping
inManagedObjectContext:(NSManagedObjectContext *)context
incrementalData:(BOOL)incrementalData
{
NSParameterAssert([mapping isKindOfClass:[EKManagedObjectMapping class]]);
NSParameterAssert(context);
Expand All @@ -189,27 +172,13 @@ + (id)objectFromExternalRepresentation:(NSDictionary *)externalRepresentation
externalRepresentation:externalRepresentation
context:context];
return [[self mapperWithImporter:importer] objectFromExternalRepresentation:externalRepresentation
withMapping:mapping
incrementalData:incrementalData];
}

+ (id) fillObject:(id)object
fromExternalRepresentation:(NSDictionary *)externalRepresentation
withMapping:(EKManagedObjectMapping *)mapping
inManagedObjectContext:(NSManagedObjectContext *)context
{
return [self fillObject:object
fromExternalRepresentation:externalRepresentation
withMapping:mapping
inManagedObjectContext:context
incrementalData:NO];
withMapping:mapping];
}

+ (id) fillObject:(id)object
fromExternalRepresentation:(NSDictionary *)externalRepresentation
withMapping:(EKManagedObjectMapping *)mapping
inManagedObjectContext:(NSManagedObjectContext*)context
incrementalData:(BOOL)incrementalData
{
NSParameterAssert([mapping isKindOfClass:[EKManagedObjectMapping class]]);
NSParameterAssert(context);
Expand All @@ -219,24 +188,12 @@ + (id) fillObject:(id)object
context:context];
return [[self mapperWithImporter:importer] fillObject:object
fromExternalRepresentation:externalRepresentation
withMapping:mapping
incrementalData:incrementalData];
}

+ (NSArray *)arrayOfObjectsFromExternalRepresentation:(NSArray *)externalRepresentation
withMapping:(EKManagedObjectMapping *)mapping
inManagedObjectContext:(NSManagedObjectContext *)context
{
return [self arrayOfObjectsFromExternalRepresentation:externalRepresentation
withMapping:mapping
inManagedObjectContext:context
incrementalData:NO];
withMapping:mapping];
}

+ (NSArray *)arrayOfObjectsFromExternalRepresentation:(NSArray *)externalRepresentation
withMapping:(EKManagedObjectMapping *)mapping
inManagedObjectContext:(NSManagedObjectContext*)context
incrementalData:(BOOL)incrementalData
{
NSParameterAssert([mapping isKindOfClass:[EKManagedObjectMapping class]]);
NSParameterAssert(context);
Expand All @@ -246,27 +203,13 @@ + (NSArray *)arrayOfObjectsFromExternalRepresentation:(NSArray *)externalReprese
context:context];

return [[self mapperWithImporter:importer] arrayOfObjectsFromExternalRepresentation:externalRepresentation
withMapping:mapping
incrementalData:incrementalData];
}

+ (NSArray *)syncArrayOfObjectsFromExternalRepresentation:(NSArray *)externalRepresentation
withMapping:(EKManagedObjectMapping *)mapping
fetchRequest:(NSFetchRequest *)fetchRequest
inManagedObjectContext:(NSManagedObjectContext *)context
{
return [self syncArrayOfObjectsFromExternalRepresentation:externalRepresentation
withMapping:mapping
fetchRequest:fetchRequest
inManagedObjectContext:context
incrementalData:NO];
withMapping:mapping];
}

+ (NSArray *)syncArrayOfObjectsFromExternalRepresentation:(NSArray *)externalRepresentation
withMapping:(EKManagedObjectMapping *)mapping
fetchRequest:(NSFetchRequest *)fetchRequest
inManagedObjectContext:(NSManagedObjectContext *)context
incrementalData:(BOOL)incrementalData
{
NSParameterAssert([mapping isKindOfClass:[EKManagedObjectMapping class]]);
NSParameterAssert(context);
Expand All @@ -276,8 +219,7 @@ + (NSArray *)syncArrayOfObjectsFromExternalRepresentation:(NSArray *)externalRep
context:context];
return [[self mapperWithImporter:importer] syncArrayOfObjectsFromExternalRepresentation:externalRepresentation
withMapping:mapping
fetchRequest:fetchRequest
incrementalData:incrementalData];
fetchRequest:fetchRequest];
}

@end
18 changes: 0 additions & 18 deletions EasyMapping/EKMapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,24 +56,6 @@
fromExternalRepresentation:(NSDictionary *)externalRepresentation
withMapping:(EKObjectMapping *)mapping;

/**
Fills previously existed object with values, provided in JSON representation. All values, that are included in mapping and were filled prior to calling this method, will be overwritten. If incrementalData is true, to-many relationship data is pushed to the existing data instead of replaced.
@param object Object to fill
@param externalRepresentation JSON representation of object data
@param mapping object mapping
@param incrementalData Defines if to-many relationship data is pushed or replaced
@result filled object
*/
+ (id) fillObject:(id)object
fromExternalRepresentation:(NSDictionary *)externalRepresentation
withMapping:(EKObjectMapping *)mapping
incrementalData:(BOOL)incrementalData;

/**
Convenience method to create array of objects from JSON.
Expand Down
Loading

0 comments on commit 9751407

Please sign in to comment.