Sha256: 4ce0738b651ec97be0ff18987b5b989af2fa7c24dd586e2aaaf692fa09fbc256

Contents?: true

Size: 1.82 KB

Versions: 8

Compression:

Stored size: 1.82 KB

Contents

#import "OCSInstanceCreator.h"
#import "OCSException.h"

@interface OCSInstanceCreator ()

@property(nonatomic, strong) NSString* instanceName;
@property(nonatomic, strong) NSString* className;
@property(nonatomic, strong) NSArray* args;
@property(nonatomic, strong) id instance;
@property(nonatomic, strong) NSString* result;

@end

@implementation OCSInstanceCreator

+(id) instanceWithClassName:(NSString*) givenClassName
                    andArgs:(NSArray*) givenArgs {
    OCSInstanceCreator* instance = [self new];
    instance.className = givenClassName;
    instance.args = givenArgs;
    return instance;
}

-(id) create {
    if([self classToCreate]) {
        [self attemptCreateWithTryCatch];
    } else {
        [self handleInvalidClassException];
    }
    return self.instance;
}

-(void) attemptCreateWithTryCatch {
    @try {
        [self attemptCreate];
    }
    @catch (NSException* exception) {
        [self handleCouldNotInvokeContructorException];
    }
}

-(void) attemptCreate {
    switch ([self.args count]) {
        case 0:
            self.instance = [[[self classToCreate] alloc] init];
            break;
        case 1:
            self.instance = [[[self classToCreate] alloc] initWithString: [self.args objectAtIndex: 0]];
            break;
        default:
            self.instance = [[[self classToCreate] alloc] initWithArray: self.args];
            break;
    }
    self.result = @"OK";
}

-(void) handleInvalidClassException {
    self.result = [[OCSException exceptionWithMessage: @"NO_CLASS %@.", self.className] stringValue];
}

-(void) handleCouldNotInvokeContructorException {
    self.result = [[OCSException exceptionWithMessage: @"COULD_NOT_INVOKE_CONSTRUCTOR %@[%i].", self.className, (int)[self.args count]] stringValue];
}

-(Class) classToCreate {
    return NSClassFromString(self.className);
}

@end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
xcfit-0.9.0 XCFitDemo/Pods/cslim/src/ExecutorObjectiveC/OCSInstanceCreator.m
xcfit-0.8.0 XCFitDemo/Pods/cslim/src/ExecutorObjectiveC/OCSInstanceCreator.m
xcfit-0.7.0 XCFitDemo/Pods/cslim/src/ExecutorObjectiveC/OCSInstanceCreator.m
xcfit-0.6.0 XCFitDemo/Pods/cslim/src/ExecutorObjectiveC/OCSInstanceCreator.m
xcfit-0.5.0 XCFitDemo/Pods/cslim/src/ExecutorObjectiveC/OCSInstanceCreator.m
xcfit-0.4.0 XCFitDemo/Pods/cslim/src/ExecutorObjectiveC/OCSInstanceCreator.m
xcfit-0.3.0 XCFitDemo/Pods/cslim/src/ExecutorObjectiveC/OCSInstanceCreator.m
xcfit-0.2.0 XCFitDemo/Pods/cslim/src/ExecutorObjectiveC/OCSInstanceCreator.m