Skip to content

Commit

Permalink
Added a separate method that takes variadic args
Browse files Browse the repository at this point in the history
  • Loading branch information
jdewind committed Jun 12, 2012
1 parent b872a58 commit d2d2876
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
3 changes: 2 additions & 1 deletion Source/JSObjectionInjector.h
Expand Up @@ -10,5 +10,6 @@
- (id)initWithContext:(NSDictionary *)theGlobalContext;
- (id)initWithContext:(NSDictionary *)theGlobalContext andModule:(JSObjectionModule *)theModule;
- (id)initWithContext:(NSDictionary *)theGlobalContext andModules:(NSArray *)modules;
- (id)getObject:(id)classOrProtocol, ...;
- (id)getObject:(id)classOrProtocol;
- (id)getObjectWithArgs:(id)classOrProtocol, ... NS_REQUIRES_NIL_TERMINATION;
@end
21 changes: 13 additions & 8 deletions Source/JSObjectionInjector.m
Expand Up @@ -69,23 +69,23 @@ - (id)initWithContext:(NSDictionary *)theGlobalContext andModules:(NSArray *)mod
}


- (id)getObject:(id)classOrProtocol, ... {
- (id)getObjectWithArgs:(id)classOrProtocol, ... {
@synchronized(self) {
if (!classOrProtocol) {
return nil;
}

NSString *key = nil;
if (class_isMetaClass(object_getClass(classOrProtocol))) {
key = NSStringFromClass(classOrProtocol);
} else {
key = [NSString stringWithFormat:@"<%@>", NSStringFromProtocol(classOrProtocol)];
}


id<JSObjectionEntry> injectorEntry = [_context objectForKey:key];
injectorEntry.injector = self;

if (!injectorEntry) {
id<JSObjectionEntry> entry = [_globalContext objectForKey:key];
if (entry) {
Expand All @@ -94,19 +94,24 @@ - (id)getObject:(id)classOrProtocol, ... {
[_context setObject:injectorEntry forKey:key];
}
}

if (classOrProtocol && injectorEntry) {
va_list va_arguments;
va_start(va_arguments, classOrProtocol);
NSArray *arguments = JSObjectionUtils.transformVariadicArgsToArray(va_arguments);
va_end(va_arguments);
return [injectorEntry extractObject:arguments];
}

return nil;
}

return nil;

}

- (id)getObject:(id)classOrProtocol {
return [self getObjectWithArgs:classOrProtocol, nil];
}

#pragma mark - Private
Expand Down
1 change: 1 addition & 0 deletions Source/JSObjectionUtils.m
Expand Up @@ -64,6 +64,7 @@ static JSObjectionPropertyInfo FindClassOrProtocolForProperty(objc_property_t pr

static NSArray* TransformVariadicArgsToArray(va_list va_arguments) {
NSMutableArray *arguments = [NSMutableArray array];

id object;
while ((object = va_arg( va_arguments, id ))) {
[arguments addObject:object];
Expand Down
2 changes: 1 addition & 1 deletion Specs/InitializerSpecs.m
Expand Up @@ -17,7 +17,7 @@
});

it(@"will override the default arguments if arguments are passed to the injector", ^{
ViewController *controller = [injector getObject:[ViewController class], @"AnotherNib", @"pretendBundle", nil];
ViewController *controller = [injector getObjectWithArgs:[ViewController class], @"AnotherNib", @"pretendBundle", nil];

[[controller.nibName should] equal:@"MyNib"];
[[controller.bundle should] equal:@"pretendBundle"];
Expand Down

0 comments on commit d2d2876

Please sign in to comment.