objc.NSManagedObjectBuilder-body.mustache Maven / Gradle / Ivy
{{#models}}{{#model}}
#import "{{classname}}ManagedObjectBuilder.h"
/**
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/
@implementation {{classname}}ManagedObjectBuilder
-(instancetype)init {
self = [super init];
if (self != nil) {
{{#isArray}} _entriesBuilder = [[{{arrayModelType}}ManagedObjectBuilder alloc] init];
{{/isArray}}{{#vars}}{{#complexType}}{{^isMap}} _{{name}}Builder = [[{{complexType}}ManagedObjectBuilder alloc] init];
{{/isMap}}{{/complexType}}{{/vars}} }
return self;
}
-({{classname}}ManagedObject*)createNew{{classname}}ManagedObjectInContext:(NSManagedObjectContext*)context {
{{classname}}ManagedObject *managedObject = [NSEntityDescription insertNewObjectForEntityForName:NSStringFromClass([{{classname}}ManagedObject class]) inManagedObjectContext:context];
return managedObject;
}
-({{classname}}ManagedObject*){{classname}}ManagedObjectFrom{{classname}}:({{classname}}*)object context:(NSManagedObjectContext*)context {
{{classname}}ManagedObject* new{{classname}} = [self createNew{{{classname}}}ManagedObjectInContext:context];
[self update{{classname}}ManagedObject:new{{classname}} with{{classname}}:object];
return new{{classname}};
}
-(void)update{{classname}}ManagedObject:({{classname}}ManagedObject*)managedObject with{{classname}}:({{classname}}*)object {
if(!managedObject || !object) {
return;
}
NSManagedObjectContext* context = managedObject.managedObjectContext;
{{#vars}}{{^complexType}} managedObject.{{name}} = [object.{{name}} copy];{{/complexType}}{{#complexType}}{{#isArray}} if(managedObject.{{name}}) {
for (id object in managedObject.{{name}}) {
[context deleteObject:object];
}
}
if(object.{{name}}) {
NSMutableOrderedSet * convertedObjs = [NSMutableOrderedSet orderedSet];
for (id innerObject in object.{{name}}) {
id convertedObj = [self.{{name}}Builder {{complexType}}ManagedObjectFrom{{complexType}}:innerObject context:managedObject.managedObjectContext];
[convertedObjs addObject:convertedObj];
}
managedObject.{{name}} = convertedObjs;
}{{/isArray}}{{^isArray}}{{^isMap}}
if(!managedObject.{{name}}) {
managedObject.{{name}} = [self.{{name}}Builder {{complexType}}ManagedObjectFrom{{complexType}}:object.{{name}} context:managedObject.managedObjectContext];
} else {
[self.{{name}}Builder update{{complexType}}ManagedObject:managedObject.{{name}} with{{complexType}}:object.{{name}}];
}{{/isMap}}{{#isMap}}managedObject.{{name}} = [object.{{name}} copy];{{/isMap}}{{/isArray}}{{/complexType}}
{{/vars}}{{#isArray}} NSMutableOrderedSet * convertedObjs = [NSMutableOrderedSet orderedSet];
for (id innerObject in object) {
id convertedObj = [self.entriesBuilder {{arrayModelType}}ManagedObjectFrom{{arrayModelType}}:innerObject context:managedObject.managedObjectContext];
[convertedObjs addObject:convertedObj];
}
managedObject.entries = convertedObjs;{{/isArray}}
}
-({{classname}}*){{classname}}From{{classname}}ManagedObject:({{classname}}ManagedObject*)obj {
if(!obj) {
return nil;
}
{{classname}}* new{{classname}} = [[{{classname}} alloc] init];
[self update{{classname}}:new{{classname}} with{{classname}}ManagedObject:obj];
return new{{classname}};
}
-(void)update{{classname}}:({{classname}}*)new{{classname}} with{{classname}}ManagedObject:({{classname}}ManagedObject*)obj {
{{#isArray}} [new{{classname}} removeAllObjects];
NSMutableArray* convertedObjs = [NSMutableArray array];
for (id innerObject in obj.entries) {
id convertedObj = [self.entriesBuilder {{arrayModelType}}From{{arrayModelType}}ManagedObject:innerObject];
[convertedObjs addObject:convertedObj];
}
[new{{classname}} addObjectsFromArray:convertedObjs];
{{/isArray}}{{#vars}}{{^complexType}} new{{classname}}.{{name}} = [obj.{{name}} copy];{{/complexType}}{{#complexType}}{{#isArray}} if(obj.{{name}} != nil) {
NSMutableArray* convertedObjs = [NSMutableArray array];
for (id innerObject in obj.{{name}}) {
id convertedObj = [self.{{name}}Builder {{complexType}}From{{complexType}}ManagedObject:innerObject];
[convertedObjs addObject:convertedObj];
}
new{{classname}}.{{name}} = ({{{ datatype }}})convertedObjs;
}
{{/isArray}}{{^isArray}} new{{classname}}.{{name}} = [self.{{name}}Builder {{complexType}}From{{complexType}}ManagedObject:obj.{{name}}];{{/isArray}}{{/complexType}}
{{/vars}}
}
@end
{{/model}}
{{/models}}