Skip to content

Commit

Permalink
Basic support for initializers has been added.
Browse files Browse the repository at this point in the history
  • Loading branch information
jdewind committed Jun 13, 2012
1 parent d2d2876 commit 8e04ce7
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 6 deletions.
24 changes: 20 additions & 4 deletions Source/JSObjectionInjectorEntry.m
Expand Up @@ -4,7 +4,9 @@


@interface JSObjectionInjectorEntry() @interface JSObjectionInjectorEntry()
- (void)notifyObjectThatItIsReady: (id)object; - (void)notifyObjectThatItIsReady: (id)object;
- (id)buildObject; - (id)buildObject:(NSArray *)arguments;
- (id)argumentsForObject:(NSArray *)givenArguments;
- (SEL)initializerForObject;
@end @end




Expand All @@ -28,7 +30,7 @@ - (id)initWithClass:(Class)theClass lifeCycle:(JSObjectionInstantiationRule)theL


- (id)extractObject:(NSArray *)arguments { - (id)extractObject:(NSArray *)arguments {
if (self.lifeCycle == JSObjectionInstantiationRuleNormal || !_storageCache) { if (self.lifeCycle == JSObjectionInstantiationRuleNormal || !_storageCache) {
return [self buildObject]; return [self buildObject:arguments];
} }


return _storageCache; return _storageCache;
Expand All @@ -50,8 +52,14 @@ - (void)notifyObjectThatItIsReady:(id)object {
} }
} }


- (id)buildObject { - (id)buildObject:(NSArray *)arguments {
id objectUnderConstruction = [[[self.classEntry alloc] init] autorelease];
id objectUnderConstruction = nil;
if ([self.classEntry respondsToSelector:@selector(objectionInitializer)]) {
objectUnderConstruction = JSObjectionUtils.buildObjectWithInitializer(self.classEntry, [self initializerForObject], [self argumentsForObject:arguments]);
} else {
objectUnderConstruction = [[[self.classEntry alloc] init] autorelease];
}


if (self.lifeCycle == JSObjectionInstantiationRuleSingleton) { if (self.lifeCycle == JSObjectionInstantiationRuleSingleton) {
_storageCache = [objectUnderConstruction retain]; _storageCache = [objectUnderConstruction retain];
Expand Down Expand Up @@ -93,6 +101,14 @@ - (id)buildObject {
return objectUnderConstruction; return objectUnderConstruction;
} }


- (SEL)initializerForObject {
return NSSelectorFromString([[self.classEntry performSelector:@selector(objectionInitializer)] objectForKey:JSObjectionInitializerKey]);
}

- (NSArray *)argumentsForObject:(NSArray *)givenArguments {
return givenArguments.count > 0 ? givenArguments : [[self.classEntry performSelector:@selector(objectionInitializer)] objectForKey:JSObjectionDefaultArgumentsKey];
}

#pragma mark Class Methods #pragma mark Class Methods
#pragma mark - #pragma mark -


Expand Down
1 change: 1 addition & 0 deletions Source/JSObjectionUtils.h
Expand Up @@ -20,4 +20,5 @@ extern const struct JSObjectionUtils {
NSSet* (*buildDependenciesForClass)(Class klass, NSSet *requirements); NSSet* (*buildDependenciesForClass)(Class klass, NSSet *requirements);
NSDictionary* (*buildInitializer)(SEL selector, NSArray *arguments); NSDictionary* (*buildInitializer)(SEL selector, NSArray *arguments);
NSArray* (*transformVariadicArgsToArray)(va_list va_arguments); NSArray* (*transformVariadicArgsToArray)(va_list va_arguments);
id (*buildObjectWithInitializer)(Class klass, SEL initializer, NSArray *arguments);
} JSObjectionUtils; } JSObjectionUtils;
19 changes: 18 additions & 1 deletion Source/JSObjectionUtils.m
Expand Up @@ -81,10 +81,27 @@ static objc_property_t GetProperty(Class klass, NSString *propertyName) {
return property; return property;
} }



static id BuildObjectWithInitializer(Class klass, SEL initializer, NSArray *arguments) {
id instance = [klass alloc];
NSMethodSignature *signature = [klass instanceMethodSignatureForSelector:initializer];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
[invocation setTarget:instance];
[invocation setSelector:initializer];
for (int i = 0; i < arguments.count; i++) {
id argument = [arguments objectAtIndex:i];
[invocation setArgument:&argument atIndex:i + 2];
}
[invocation invoke];
[invocation getReturnValue:&instance];
return [instance autorelease];
}

const struct JSObjectionUtils JSObjectionUtils = { const struct JSObjectionUtils JSObjectionUtils = {
.findClassOrProtocolForProperty = FindClassOrProtocolForProperty, .findClassOrProtocolForProperty = FindClassOrProtocolForProperty,
.propertyForClass = GetProperty, .propertyForClass = GetProperty,
.buildDependenciesForClass = BuildDependenciesForClass, .buildDependenciesForClass = BuildDependenciesForClass,
.buildInitializer = BuildInitializer, .buildInitializer = BuildInitializer,
.transformVariadicArgsToArray = TransformVariadicArgsToArray .transformVariadicArgsToArray = TransformVariadicArgsToArray,
.buildObjectWithInitializer = BuildObjectWithInitializer
}; };
2 changes: 1 addition & 1 deletion Specs/InitializerSpecs.m
Expand Up @@ -19,7 +19,7 @@
it(@"will override the default arguments if arguments are passed to the injector", ^{ it(@"will override the default arguments if arguments are passed to the injector", ^{
ViewController *controller = [injector getObjectWithArgs:[ViewController class], @"AnotherNib", @"pretendBundle", nil]; ViewController *controller = [injector getObjectWithArgs:[ViewController class], @"AnotherNib", @"pretendBundle", nil];


[[controller.nibName should] equal:@"MyNib"]; [[controller.nibName should] equal:@"AnotherNib"];
[[controller.bundle should] equal:@"pretendBundle"]; [[controller.bundle should] equal:@"pretendBundle"];
[[controller.car should] beMemberOfClass:[Car class]]; [[controller.car should] beMemberOfClass:[Car class]];
}); });
Expand Down

0 comments on commit 8e04ce7

Please sign in to comment.